I'm brushing up on some Haskell and I am trying to write a permutation function that would map [1,2,3] -> [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]. I have the following -
permute:: [a] -> [[a]]
permute [] = []
permute list = map f list
where
f x = listProduct x (permute (exclude x list))
exclude e list1 = filter (/= e) list1
listProduct x list2 = map (x :) list2
The following is the error message I get -
permutations.hs:3:20:
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for permute :: [a] -> [[a]]
at permutations.hs:1:11
Expected type: a -> [a]
Actual type: a -> [[a]]
In the first argument of `map', namely `f'
In the expression: map f list
In an equation for `permute':
permute list
= map f list
where
f x = listProduct x (permute (exclude x list))
exclude e list1 = filter (/= e) list1
listProduct x list2 = map (x :) list2
Failed, modules loaded: none.
I would try to debug, but it doesn't even compile. Any ideas?
fto be a function of type a -> [a] but is getting a type a -> [[a]] - גלעד ברקןlistProductanything looks like it ought to return a[[a]], which would makeftake the argumentxand return a[[a]]. Perhaps try to experiment with each function independently to make sure its doing what you'd like it to. - גלעד ברקןmap :: (a->b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs- Dan Sokf,excludeandlistProduct. The compiler errors when you have type signatures will give you more of a clue about where you went wrong. - TheCriticalImperitiveScopedTypeVariablesextension, and doesn't actually help any. - Ørjan Johansen