fork download
  1. open List;;
  2.  
  3. let rec flipfold f h acc = function
  4. | [] -> acc
  5. | y::ys -> flipfold h f (f y acc) ys;;
  6.  
  7. let flipmap f h = flipfold (fun x a -> (f x)::a) (fun y a -> (h y)::a) [];;
  8.  
  9. let rec wordpath alphabet length =
  10. if length <= 1 then map (fun x -> [x]) alphabet
  11. else
  12. let prep xs x = map (fun y -> x::y) @@ wordpath xs (length - 1) in
  13. flatten @@ flipmap (prep alphabet) (prep @@ rev alphabet) alphabet;;
  14.  
  15. let rec print_path = function
  16. | [] -> ()
  17. | x::xs -> iter print_char x; print_char '\n'; print_path xs;;
  18.  
  19. print_path @@ wordpath ['a'; 'b'; 'c'] 3;;
Success #stdin #stdout 0s 4212KB
stdin
Standard input is empty
stdout
cca
ccb
ccc
cbc
cbb
cba
caa
cab
cac
bac
bab
baa
bba
bbb
bbc
bcc
bcb
bca
aca
acb
acc
abc
abb
aba
aaa
aab
aac