如何修复功能列表中的排列错误?
> :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)]
答案 0 :(得分:3)
在Haskell中1
的类型是
1 :: Num a => a
因此,Haskell无法决定在您的案例中选择a
。您可以使用简单的类型签名来解决此问题
perms :: Num a => [[a -> a]]
perms = permutations [(+1),(subtract 2),(*3)]