深入理解柯里化的例子
Example of deep understanding of currying
阅读https://wiki.haskell.org/Currying
它指出:
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.
什么是 Haskell technique/idiom 需要更深入地了解柯里化?
偏函数应用并不是 Haskell 的显着特征;这只是柯里化函数的结果。
map :: (a -> b) -> [a] -> [b]
在像Python这样的语言中,map
总是接受两个参数:一个类型为a -> b
的函数和一个类型为[a]
map(f, [x, y, z]) == [f(x), f(y), f(z)]
这需要你假装 ->
语法只是为了展示,(a -> b)
和 [a]
之间的 ->
与[a] -> [b]
之间的一个。然而,事实并非如此;它是 exact 相同的运算符,并且是右结合的。 map
的类型可以显式括号为
map :: (a -> b) -> ([a] -> [b])
突然间,您可能只给 map
一个参数(函数)并返回一个类型为 [=22 的 new 函数似乎没那么有趣了=].这就是所有的部分函数应用是:利用所有函数都被柯里化的事实。
事实上,您永远不会真正为一个函数提供多个参数。为了让 ->
是右关联的,函数 application 是 left 关联的,这意味着 "multi-argument" 调用像
map f [1,2,3]
其实是两个函数的应用,加上括号就更清楚了
(map f) [1,2,3]
map
是第一个 "partially" 应用于一个参数 f
,returns 一个新函数。 然后将此函数应用于[1,2,3]
以获得最终结果。
阅读https://wiki.haskell.org/Currying
它指出:
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.
什么是 Haskell technique/idiom 需要更深入地了解柯里化?
偏函数应用并不是 Haskell 的显着特征;这只是柯里化函数的结果。
map :: (a -> b) -> [a] -> [b]
在像Python这样的语言中,map
总是接受两个参数:一个类型为a -> b
的函数和一个类型为[a]
map(f, [x, y, z]) == [f(x), f(y), f(z)]
这需要你假装 ->
语法只是为了展示,(a -> b)
和 [a]
之间的 ->
与[a] -> [b]
之间的一个。然而,事实并非如此;它是 exact 相同的运算符,并且是右结合的。 map
的类型可以显式括号为
map :: (a -> b) -> ([a] -> [b])
突然间,您可能只给 map
一个参数(函数)并返回一个类型为 [=22 的 new 函数似乎没那么有趣了=].这就是所有的部分函数应用是:利用所有函数都被柯里化的事实。
事实上,您永远不会真正为一个函数提供多个参数。为了让 ->
是右关联的,函数 application 是 left 关联的,这意味着 "multi-argument" 调用像
map f [1,2,3]
其实是两个函数的应用,加上括号就更清楚了
(map f) [1,2,3]
map
是第一个 "partially" 应用于一个参数 f
,returns 一个新函数。 然后将此函数应用于[1,2,3]
以获得最终结果。