[Haskell-cafe] Re: Generic permutations

Cetin Sert cetin.sert at gmail.com
Sat Jan 26 05:07:30 EST 2008


I have come up with this myself ^_^

mps :: [a] → [[a]] → [[a]]
mps []     _   = []
mps _      []  = []
mps (x:xs) yss = map (x:) yss ++ mps xs yss

pms :: [a] → Int → [[a]]
pms [] _  = [[]]
pms _  0  = [[]]
pms xxs n = mps xxs (pms (xxs) (n - 1))

-- now bpms can pointlessly be redefined as

bpms = pms [False,True]

On 26/01/2008, Cetin Sert <cetin.sert at gmail.com> wrote:
>
> How can I make a generic permutations function?
>
> -- boolean permutations
> bpms :: Int → [[Bool]]
> bpms 0 = [[]]
> bpms n = map (False:) bss ++ map (True:) bss
>          where
>            bss = bpms (n - 1)
>
> -- generic permutations
> pms a :: Int → [[a]]
>
> Best Regards,
> Cetin Sert
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080126/cc6bb960/attachment.htm


More information about the Haskell-Cafe mailing list