Haskell 标识符中的撇号 [英] Apostrophe in identifiers in Haskell
问题描述
我发现这段代码在互联网上被截断了:
I found this code snipped on the internet:
digits 0 = [0]
digits n = digits' n []
where digits' 0 ds = ds
digits' n ds = let (q,r) = quotRem n 10
in digits' q (r:ds)
sumOfDigits = sum . digits
有人能快速解释一下递归函数调用后的'"符号( digits n = digits' n []
)是干什么用的?我在 Haskell(教程)中看到了一些其他的代码示例,但我不明白这个.一个快速的解释表示赞赏.
Can someone quickly explain what the " ' " sign ( digits n = digits' n []
) after the recursive function call is for? I've seen some other code examples in Haskell (tutorials), but im not understandig this one. A quick explanation is appreciated.
推荐答案
撇号只是名称的一部分.它是 Haskell 中采用的命名约定(惯用语).
The apostrophe is just part of the name. It is a naming convention (idiom) adopted in Haskell.
Haskell 中的约定是,就像在数学中,撇号变量名上的变量表示与先前变量有某种关联或相似的变量.
The convention in Haskell is that, like in math, the apostrophe on a variable name represents a variable that is somehow related, or similar, to a prior variable.
示例:
let x = 1
x' = x * 2
in x'
x'
与 x
相关,我们用撇号表示.
x'
is related to x
, and we indicate that with the apostrophe.
顺便说一下,你可以在 GHCi 中运行它,
You can run this in GHCi, by the way,
Prelude> :{
Prelude| let x = 1
Prelude| x' = x * 2
Prelude| in x'
Prelude| :}
2
这篇关于Haskell 标识符中的撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!