Haskell高阶函数来计算长度 [英] Haskell higher order function to calculate length

查看:115
本文介绍了Haskell高阶函数来计算长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解这里发生了什么?任何人都可以解释这段代码吗?
这个函数如何计算长度?

  callength = foldr(\_ n  - > 1 + n )0 

为什么它使用lambda,下划线,下划线和n之间的空格,右边是零侧?

只是意味着一个函数接受两个参数,并返回比第二个参数多一个的参数。下划线仅表示参数被忽略。为了比较,作为不使用通配符模式的顶层声明(下划线),该函数如下所示:

  foo xn = 1 + n 

现在,这里有一个示例列表:

  [1,2,3,4] 





  1:2:3:4:[] 

什么 foldr does递归地替换每个 (:)与给定的函数以及 [] 函数之后的参数() 。因此,对于任何 f z, foldr fz [1,2,3,4] 看起来像这样:

  f 1(f 2(f 3(f 4 z))) 

(这就是为什么 foldr(:) [] 返回你给它的同样的列表 - 它会重建原来的列表结构。)

foo 和零0,它看起来像:

  foo 1(foo 2(foo 3 foo 4 0)))

我们知道 foo 忽略它的第一个参数,并返回比第二个参数多一个。因此,这与以下内容相同:

  1 +(1 +(1 +(1 + 0)))

这是4,列表的长度。基本上,折叠会忽略列表中的每个元素,并为每个元素添加一个累加器,给出长度。 0用于结束整个过程,因为空列表的长度为0.



为了更详细地了解这一点,我们可以扩展每个调用步骤,

  foldr foo 0(1:2:3:4:[])
foo 1( (2:3:4:[]))
1 + foldr foo 0(2:3:4:[])
1 + foo 2(foldr foo 0(3:4: []))
1 + 1 + foldr foo 0(3:4:[])
1 + 1 + foo 3(foldr foo 0(4:[]))
1 + 1 + 1 + foldr foo 0(4:[])
1 + 1 + 1 + foo 4(foldr foo 0 [])
1 + 1 + 1 + 1 + foldr foo 0 []
1 + 1 + 1 + 1 + 0
1 + 1 + 1 + 1
1 + 1 + 2
1 + 3
4


I can't understand what's happening here? Can anybody please explain this code? How does this function calculate the length?

callength = foldr (\_ n -> 1 + n) 0

Why does it use lambda, underscore, space between underscore and n and zero on the right hand side?

解决方案

(\_ n -> 1 + n) simply means a function that takes two arguments, and returns one more than its second argument. The underscore simply means that a parameter is ignored. For comparison, as a top-level declaration without using a wildcard pattern (the underscore), this function would look like:

foo x n = 1 + n

Now, here's an example list:

[1, 2, 3, 4]

This is actually just syntactic sugar for:

1 : 2 : 3 : 4 : []

What foldr does is recursively replace each (:) with the function it's given, and the [] with the argument after the function (the zero). So, foldr f z [1, 2, 3, 4] for any f and z looks like this:

f 1 (f 2 (f 3 (f 4 z)))

(This is why foldr (:) [] just returns back the same list you give it — it ends up reconstructing the original list structure.)

In this case, using the function foo and the zero 0, it looks like:

foo 1 (foo 2 (foo 3 (foo 4 0)))

We know that foo ignores its first argument, and returns one more than its second argument. So this is the same as:

1 + (1 + (1 + (1 + 0)))

which is 4, the length of the list. Basically, the fold ignores every element of the list, and just adds one to an accumulator for every element, giving the length. The 0 is used to end the whole process, and because the length of the empty list is 0.

To see this in more detail, we can expand each call step-by-step:

foldr foo 0 (1 : 2 : 3 : 4 : [])
foo 1 (foldr foo 0 (2 : 3 : 4 : []))
1 + foldr foo 0 (2 : 3 : 4 : [])
1 + foo 2 (foldr foo 0 (3 : 4 : []))
1 + 1 + foldr foo 0 (3 : 4 : [])
1 + 1 + foo 3 (foldr foo 0 (4 : []))
1 + 1 + 1 + foldr foo 0 (4 : [])
1 + 1 + 1 + foo 4 (foldr foo 0 [])
1 + 1 + 1 + 1 + foldr foo 0 []
1 + 1 + 1 + 1 + 0
1 + 1 + 1 + 1
1 + 1 + 2
1 + 3
4

这篇关于Haskell高阶函数来计算长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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