在Haskell中使用foldr与foldr1的用法 [英] foldr vs foldr1 usage in Haskell

查看:658
本文介绍了在Haskell中使用foldr与foldr1的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写:

 >让xs = [1,5,19,2,-3,5] 
>最大0 xs
19

> foldr1 max xs
19

如果我写(我知道,初始值不正确这里是一个泛型最大函数...):

 >让最大值'= foldr max 0 
>最大'xs
19

但是,如果我写道:

 >让maximum2'= foldr1 max 
>最大2'xs

回应是:

< interactive>:61:11:
无法与实际类型为'Integer'的$(b)匹配预期类型
期望类型:[()]
实际类型:[Integer]
在maximum2'的第一个参数中,即`xs'
在表达式中:maximum2'xs

我是Haskell的新手。我究竟做错了什么? (无法解密错误信息...)如何使用 foldr1 max

编辑(接受答案后):

为了展示更多违约规则的例子,效果(答案也解释了这些):

示例1:

 > let max'= max 
> :t max
max :: Ord a => a - > a - > a

> :t max'
max'::() - > () - > ()

示例2:

 >让加=(+)
> :t(+)
(+):: Num a => a - > a - > a

> :t加
plus ::整数 - >整数 - >整数


解决方案

问题是关于类型和GHCi的违约。 max 的类型是多态的:

 > :t max 
max :: Ord a => a - > a - > a

最大值的情况下,编译器可以看到 a 是某种编号,而GHCi默认编号为整数:

 > :t最大值'
最大值':: [Integer] - >整数

maximum2'的情况下线索较少,并且默认 a 为单位类型:

 > ; :t maximum2'
maximum2':: [()] - > ()

如果您提供了一个类型签名,一切都很好:

 >让maximum3':: Ord a => [a]  - >一个 ; maximum3'= foldr1 max 
> :t maximum3'
maximum3':: Ord a => [a] - > a

我认为GHCi的违约规则可以使其他类型的情况更容易省略 - 请参阅 http://www.haskell.org/ghc/docs /7.6.2/html/users_guide/interactive-evaluation.html#id484837


If I write:

> let xs = [1,5,19,2,-3,5]
> foldr max 0 xs
19

> foldr1 max xs
19

And if I write (I know, the initial value is incorrect here for a generic maximum function...):

> let maximum' = foldr max 0
> maximum' xs
19

But if I write:

> let maximum2' = foldr1 max
> maximum2' xs

the response is:

<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

I am new to Haskell. What am I doing wrong? (Can't decipher the error message...) How to use foldr1 with max? Thanks.

EDIT (AFTER ACCEPTING ANSWER):

Just to show some more examples of the defaulting rules' effect (the answer explains these, too):

Example 1:

> let max' = max
> :t max
max :: Ord a => a -> a -> a

> :t max' 
max' :: () -> () -> ()

Example 2:

> let plus = (+)
> :t (+)
(+) :: Num a => a -> a -> a

> :t plus 
plus :: Integer -> Integer -> Integer

解决方案

The problem is to do with the polymorphism of the types and GHCi's defaulting. The type of max is polymorphic:

> :t max
max :: Ord a => a -> a -> a

In the case of maximum', the compiler can see that a is some sort of number, and GHCi defaults the number to Integer:

> :t maximum'
maximum' :: [Integer] -> Integer

In the case of maximum2' it has less clues, and defaults a to the unit type:

> :t maximum2'
maximum2' :: [()] -> ()

If you provide a type signature, all is well:

> let maximum3' :: Ord a => [a] -> a ; maximum3' = foldr1 max
> :t maximum3'
maximum3' :: Ord a => [a] -> a

I think GHCi's defaulting rules are there to make certain other cases where the types are omitted easier -- see http://www.haskell.org/ghc/docs/7.6.2/html/users_guide/interactive-evaluation.html#id484837

这篇关于在Haskell中使用foldr与foldr1的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆