为什么这个最大值的实现在haskell中不起作用:最大值' = foldr1 max

时间:2016-12-12 22:04:33

标签: haskell currying

此实现工作正常:

 λ > let maximum' xs = foldl1 max xs
 λ > maximum' [1..5]
 5

由于currying,我们可以将foo a = bar b a之类的函数重写为foo = bar b

foldl1 max预期类型的​​结果

 λ > :t foldl1 max
 foldl1 max :: Ord a => [a] -> a

但这不起作用

 λ > let maximum' = foldl1 max
 λ > maximum' [1..5]

<interactive>:71:11:
    No instance for (Num ()) arising from the literal `1'
    Possible fix: add an instance declaration for (Num ())
    In the expression: 1
    In the first argument of maximum', namely `[1 .. 5]'
    In the expression: maximum' [1 .. 5]

在这种情况下,这个错误对我来说并不清楚。那么,为什么会出现这种错误?有没有办法解决它并定义这样的函数而不明确指定最后一个参数?

0 个答案:

没有答案