深入了解curry的示例 [英] Example of deep understanding of currying

查看:60
本文介绍了深入了解curry的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 https://wiki.haskell.org/Currying

它表明:

在很多时候,新程序员可以忽略currying.这将所有功能视为咖喱的主要优势是理论上:所有功能都经过处理后,形式化证明会更容易统一(输入一个参数,输出一个结果).话虽如此,那里是您需要了解的Haskell成语和技术curr吟.

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技术/习惯用法,需要更深入地了解咖喱?

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

推荐答案

部分函数应用程序并不是Haskell的独特功能.这仅仅是功能强大的结果.

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

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

在像Python这样的语言中, map 始终带有两个参数:类型为 a->的函数.b [a]

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)]

这需要您假装-> 语法仅用于显示,并且-> (a-> b)之间 [a] [a]->[b] .但是,事实并非如此.它是 exact 相同的运算符,并且是右关联的.可以将 map 的类型显式括入

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])

突然间,您可能只给 map 一个参数(该函数)并返回了 [type]类型的 new 函数,似乎就没那么有趣了.]->[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.

实际上,您从未真正给一个函数提供多个参数.为了使-> 是右关联的,功能 application left 关联的,这意味着像"

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 首先被部分地"应用于一个参数 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.

这篇关于深入了解curry的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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