Haskell : permutation fxn using Monadic lifters

import Control.Monad
combo :: [[a]]->[[a]]->[[a]]
combo = liftM2 (++)

combiner :: [[a]]->[[a]]
combiner xs = (foldr (.) id $ take (n-1) $ cycle [combo xs] ) xs
where n = length xs

this does permutations
samples —
combiner [“a”,”b”]
[“aa”,”ab”,”ba”,”bb”]

combiner [“a”,”b”,”c”]
[“aaa”,”aab”,”aac”,”aba”,”abb”,”abc”,”aca”,”acb”,
“acc”,”baa”,”bab”,”bac”,”bba”,”bbb”,”bbc”,”bca”,”bcb”,”bcc”,”caa”,”cab”,”cac”,
“cba”,”cbb”,”cbc”,”cca”,”ccb”,”ccc”]

combiner [“a”]
[“a”]

combiner []
[]

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s