Skip to content

Instantly share code, notes, and snippets.

@varreli
Last active May 17, 2018 19:05
Show Gist options
  • Save varreli/13eb5adcddc52ae87847156389b0f5e1 to your computer and use it in GitHub Desktop.
Save varreli/13eb5adcddc52ae87847156389b0f5e1 to your computer and use it in GitHub Desktop.
variation of function choices
import Debug.Trace
subs :: [a] -> [[a]]
subs [] = [[]]
subs (x:xs) = yss ++ map (x:) yss
where yss = subs xs
interleave :: a -> [a] -> [[a]]
interleave x [] = [[x]]
interleave x (y:ys) = (x:y:ys) : map (y:) (interleave x ys)
perms :: [a] -> [[a]]
perms [] = [[]]
perms (x:xs) = trace "now it terminates" concat (map (interleave x) (perms xs))
choices :: [a] -> [[a]]
choices = concat . map perms . subs
choices' :: [a] -> [[a]]
choices' xs = [ list | yss <- subs xs,
list <- perms yss ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment