list permutations in haskell
- by turingcomplete
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