Skip to content

Instantly share code, notes, and snippets.

@oropon
Last active January 4, 2016 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oropon/8540769 to your computer and use it in GitHub Desktop.
Save oropon/8540769 to your computer and use it in GitHub Desktop.
module Main where
import System.Random
import Data.Maybe
import Data.Bits
import Data.List
import Data.Char
main = do
gen <- getStdGen
print $ nub $ take 1000 $ barcodes avail_chrs 8 (randomRs (0,maxBound::Int) gen :: [Int])
avail_chrs :: [Char]
avail_chrs = filter (/= 'O') ['A'..'Z']
barcodes :: [Char] -> Int -> [Int] -> [[Char]]
barcodes comps len ns = (map (avail_chrs !!) (seg ++ [check_digit])) : barcodes comps len segs
where
seg = map (flip mod $ length avail_chrs) $ take (len-1) ns
segs = drop (len-1) ns
check_digit = get_check_digit (length avail_chrs) seg
get_check_digit :: Int -> [Int] -> Int
get_check_digit max = flip mod max . foldl1 xor
unique :: (Eq a) => [a] -> [a]
unique [] = []
unique (x:xs)
| x `elem` xs = unique xs
| otherwise = x: unique xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment