input_file tmp cocacola type Code String read_codes IO Code read_codes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
input_file = "/tmp/cocacola"
type Code = String
read_codes :: IO [Code]
read_codes = do
ls <- readFile input_file
return $ lines ls
possible_codes :: [Code]
possible_codes = gen_word 8 ['A'..'Z']
where
gen_word :: Int -> [Char] -> [Code]
gen_word 0 _ = [""]
gen_word len alphabet = concat $ map (\c -> map (\rest -> c:rest) rests) alphabet
where rests = gen_word (len - 1) alphabet
checks :: [Code -> Bool]
checks = [(\c -> head c == 'B') ]
generate_codes :: [Code] -> [Code]
generate_codes init_codes =
filter passes_checks codes
where
passes_checks code = all (\test -> test code) checks
codes = possible_codes
--main :: IO ()
main = do
known_codes <- read_codes
mapM putStrLn $ generate_codes known_codes