list permutations in haskell

Posted by turingcomplete on Stack Overflow See other posts from Stack Overflow or by turingcomplete
Published on 2012-07-06T08:54:06Z Indexed on 2012/07/06 9:15 UTC
Read the original article Hit count: 213

Filed under:
|
|

So I'm new to haskell and I've been playing with it for a while now. I want to get my function that outputs all list permutations to work. I have written 2 implementations, one works well, the other is giving me an error. Any help would be awesome.

This is the first (working) implementation:

permute [] = [[]]
permute xs = [y| x <- xs, y <- map (x:) $ permute $ delete x xs]

This one is giving me an error:

permute [] = [[]]
permute xs = map (\x -> map (x:) $ permute $ delete x xs) xs

and here's the error message:

Occurs check: cannot construct the infinite type: t0 = [t0]
Expected type: [t0]
Actual type: [[t0]]
In the expression: map (x :) $ permute $ delete x xs
In the first argument of `map', namely
`(\ x -> map (x :) $ permute $ delete x xs)'

I'd appreciate if someone could explain why I'm getting this error. Thanks

© Stack Overflow or respective owner

Related posts about list

Related posts about haskell