1

How to fix the permutation error on a list of functions?

> :m + Data.List > permutations [(+1),(-2),(*3)] No instance for (Num a0) arising from a use of `+' The type variable `a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Num Double -- Defined in `GHC.Float' instance Num Float -- Defined in `GHC.Float' instance Integral a => Num (GHC.Real.Ratio a) -- Defined in `GHC.Real' ...plus three others In the expression: (+ 1) In the first argument of `permutations', namely `[(+ 1), (- 2), (* 3)]' In the expression: permutations [(+ 1), (- 2), (* 3)] 
1
  • 1
    Never forget that (-2) is not a section, but equivalent to (negate 2) Commented Sep 30, 2013 at 23:13

1 Answer 1

3

In Haskell the type of 1 is

1 :: Num a => a 

So Haskell can't decide what a to choose in your case. You can fix this with a simple type signature

perms :: Num a => [[a -> a]] perms = permutations [(+1),(subtract 2),(*3)] 
Sign up to request clarification or add additional context in comments.

2 Comments

The type should be Num a => [[a -> a]], I think. There's also a problem with (-2) being interpreted as a number. Using subtract 2 instead fixes that.
You don't need to decide what the type of a is. In fact, your type signature doesn't change anything. The problem was simply with (-2), which will be read as Num a => a as others have stated, while the other elements of the list are Num a => a -> a.