1

如何修复函数列表中的排列错误?

> :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)]
4

1 回答 1

3

在 Haskell 中,类型1

1 :: Num a => a

因此,Haskell 无法决定a在您的情况下选择什么。您可以使用简单的类型签名来解决此问题

perms :: Num a => [[a -> a]]
perms = permutations [(+1),(subtract 2),(*3)]
于 2013-09-30T21:00:32.873 回答