' 的含义 在 Haskell 函数名中?
引用 '
的用途是什么? 我已经阅读了有关柯里化函数的内容,并阅读了定义 add 函数的两种方法 - 柯里化和非柯里化。 柯里化版本...
myadd' :: Int -> Int -> Int
myadd' x y = x + y
...但在没有引用的情况下它也同样有效。 那么'
有什么意义呢?
What is quote '
used for? I have read about curried functions and read two ways of defining the add function - curried and uncurried. The curried version...
myadd' :: Int -> Int -> Int
myadd' x y = x + y
...but it works equally well without the quote. So what is the point of the '
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这句话对哈斯克尔来说毫无意义。 它只是该函数名称的一部分。
人们倾向于将其用于“内部”功能。 如果您有一个使用累加器参数对列表求和的函数,则您的 sum 函数将采用两个参数。 这很丑陋,所以你创建了一个包含两个参数的
sum'
函数,以及一个包含一个参数的sum
函数,例如sum list = sum' 0 list
。编辑,也许我应该只显示代码:
您这样做是为了使
sum'
是尾递归的,并且“公共 API”看起来很漂亮。The quote means nothing to Haskell. It is just part of the name of that function.
People tend to use this for "internal" functions. If you have a function that sums a list by using an accumulator argument, your sum function will take two args. This is ugly, so you make a
sum'
function of two args, and asum
function of one arg likesum list = sum' 0 list
.Edit, perhaps I should just show the code:
You do this so that
sum'
is tail-recursive, and so that the "public API" is nice looking.它通常发音为“prime”,因此将是“myadd prime”。 它通常用于表示计算的下一步或替代方案。
所以,你可以说
或者
它只是一种习惯,就像在 Java、C 等的 for 循环中使用 int i 作为索引。
编辑:既然我已经添加了所有单词和代码格式,这个答案希望更有帮助。 :) 我一直忘记这不是一个所见即所得的系统!
It is often pronounced "prime", so that would be "myadd prime". It is usually used to notate a next step in the computation, or an alternative.
So, you can say
Or
It just a habit, like using int i as the index in a for loop for Java, C, etc.
Edit: This answer is hopefully more helpful now that I've added all the words, and code formatting. :) I keep on forgetting that this is not a WYSIWYG system!
在本例中,
'
字符没有特殊意义; 它只是标识符的一部分。 换句话说,myadd
和myadd'
是不同的、不相关的函数。但按照惯例,
'
用于表示某种逻辑求值关系。 因此,假设函数myadd
和myadd'
是相关的,这样myadd'
可以从myadd
派生。 这是源自学术界(Haskell 的根源)的形式逻辑和证明的惯例。 我应该强调,这只是一个约定,Haskell 并不强制执行它。There's no particular point to the
'
character in this instance; it's just part of the identifier. In other words,myadd
andmyadd'
are distinct, unrelated functions.Conventionally though, the
'
is used to denote some logical evaluation relationship. So, hypothetical functionmyadd
andmyadd'
would be related such thatmyadd'
could be derived frommyadd
. This is a convention derived from formal logic and proofs in academia (where Haskell has its roots). I should underscore that this is only a convention, Haskell does not enforce it.quote ' 只是 Haskell 名称中另一个允许的字符。 它通常用于定义函数的变体,在这种情况下,quote 发音为“prime”。 具体来说,Haskell 库使用 quote-variants 来表明变体是严格的。 例如:
foldl
是惰性的,foldl'
是严格的。在这种情况下,看起来引号只是用于分隔柯里化和非柯里化变体。
quote ' is just another allowed character in Haskell names. It's often used to define variants of functions, in which case quote is pronounced 'prime'. Specifically, the Haskell libraries use quote-variants to show that the variant is strict. For example:
foldl
is lazy,foldl'
is strict.In this case, it looks like the quote is just used to separate the curried and uncurried variants.
正如其他人所说,
'
对 Haskell 本身没有任何意义。 它只是一个字符,就像字母或数字一样。'
用于表示函数的替代版本(在foldl
和foldl'
的情况下)或辅助函数。 有时,您甚至会在函数名称上看到几个'
。 在函数名称末尾添加'
比编写someFunctionHelper
和someFunctionStrict
更加简洁。这种表示法起源于数学和物理学,如果有一个函数
f(x)
,它的导数通常表示为f'(x)
。As said by others, the
'
does not hold any meaning for Haskell itself. It is just a character, like the a letter or a number.The
'
is used to denote alternative versions of a function (in the case offoldl
andfoldl'
) or helper functions. Sometimes, you'll even see several'
on a function name. Adding a'
to the end of a function name is just much more concise than writingsomeFunctionHelper
andsomeFunctionStrict
.The origin of this notation is in mathematics and physics, where, if you have a function
f(x)
, its derivate is often denoted asf'(x)
.