我在互联网上找到了这段代码片段:
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 []
) 递归函数调用之后是for?我在 Haskell (教程)中看到了一些其他代码示例,但我不理解这个。快速解释表示赞赏。
撇号只是名称的一部分。它是 Haskell 中采用的命名约定(惯用语)。
Haskell 的约定是,就像数学一样 http://en.wikipedia.org/wiki/Prime_%28symbol%29#Use_in_mathematics.2C_statistics.2C_and_science,变量名称上的撇号表示与先前变量以某种方式相关或相似的变量。
一个例子:
let x = 1
x' = x * 2
in x'
x'
与x
,我们用撇号来表示。
顺便说一下,你可以在 GHCi 中运行它,
Prelude> :{
Prelude| let x = 1
Prelude| x' = x * 2
Prelude| in x'
Prelude| :}
2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)