Example of deep understanding of currying

北战南征 提交于 2019-12-24 08:17:21

问题


Reading https://wiki.haskell.org/Currying

it states :

Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there are Haskell idioms and techniques for which you need to understand currying.

What is a Haskell technique/idiom that a deeper understanding of currying is required ?


回答1:


Partial function application isn't really a distinct feature of Haskell; it is just a consequence of curried functions.

map :: (a -> b) -> [a] -> [b]

In a language like Python, map always takes two arguments: a function of type a -> b and a list of type [a]

map(f, [x, y, z]) == [f(x), f(y), f(z)]

This requires you to pretend that the -> syntax is just for show, and that the -> between (a -> b) and [a] is not really the same as the one between [a] -> [b]. However, that is not the case; it's the exact same operator, and it is right-associative. The type of map can be explicitly parenthesized as

map :: (a -> b) -> ([a] -> [b])

and suddenly it seems much less interesting that you might give only one argument (the function) to map and get back a new function of type [a] -> [b]. That is all partial function application is: taking advantage of the fact that all functions are curried.

In fact, you never really give more than one argument to a function. To go along with -> being right-associative, function application is left-associative, meaning a "multi-argument" call like

map f [1,2,3]

is really two function applications, which becomes clearer if we parenthesize it.

(map f) [1,2,3]

map is first "partially" applied to one argument f, which returns a new function. This function is then applied to [1,2,3] to get the final result.



来源:https://stackoverflow.com/questions/38362348/example-of-deep-understanding-of-currying

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!