本文介绍了Haskell - 柯里?需要进一步解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  addList :: [int]  - > int 
addList = foldl1(+)

为什么这会起作用? Currying部分。为什么没有变量?

解决方案

如果你定义了一个像 fxy = bla ,这与 fx = \y - >相同。 bla ,它与 f = \ x - >相同。 (\y-> bla)。换句话说, f 是一个函数,它接受一个参数 x ,并返回另一个带有一个参数的函数 y ,然后返回实际结果。这就是所谓的currying。



类似地,当您执行 fxy 时,它与 (fx)y 。即你使用参数 x 调用函数 f 。这将返回另一个函数,您将它应用于参数 y



换句话说,当您执行 addList xs = foldl1(+)xs ,您首先调用 foldl1(+),然后返回另一个函数,适用于 xs 。因此,由于 foldl1(+)返回的函数实际上与 addList 相同,所以您可以将其缩短为 addList = foldl1(+)


So something like

addList :: [int] -> int
addList = foldl1 (+)

Why does this work? The Currying part. Why no variable?

解决方案

If you define a function like f x y = bla, this is the same as f x = \y -> bla, which is the same as f = \x -> (\y -> bla). In other words f is a function which takes one argument, x, and returns another function which takes one argument, y, and then returns the actual result. This is known as currying.

Analogously when you do f x y, it's the same as (f x) y. I.e. you're calling the function f with the argument x. This returns another function, which you apply to the argument y.

So in other words when you do addList xs = foldl1 (+) xs, you're first calling foldl1 (+) which then returns another function, which you apply to xs. So since the function returned by foldl1 (+) is actually the same one as addList, you can just shorten it to addList = foldl1 (+).

这篇关于Haskell - 柯里?需要进一步解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:01