如果我写:
> let xs = [1,5,19,2,-3,5]
> foldr max 0 xs
19
> foldr1 max xs
19
如果我写(我知道,这里的初始值对于通用最大函数是不正确的......):
> let maximum' = foldr max 0
> maximum' xs
19
但如果我写:
> let maximum2' = foldr1 max
> maximum2' xs
回应是:
<interactive>:61:11:
Couldn't match expected type `()' with actual type `Integer'
Expected type: [()]
Actual type: [Integer]
In the first argument of maximum2', namely `xs'
In the expression: maximum2' xs
我是 Haskell 的新手。我究竟做错了什么?(无法破译错误信息...)如何使用foldr1
with max
?谢谢。
编辑(接受答案后):
只是为了展示更多关于默认规则效果的示例(答案也解释了这些):
示例 1:
> let max' = max
> :t max
max :: Ord a => a -> a -> a
> :t max'
max' :: () -> () -> ()
示例 2:
> let plus = (+)
> :t (+)
(+) :: Num a => a -> a -> a
> :t plus
plus :: Integer -> Integer -> Integer