Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

coding style - 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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The apostrophe is just part of the name. It is a naming convention (idiom) adopted in 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.

An example:

let x  = 1
    x' = x * 2
in x'

x' is related to x, and we indicate that with the apostrophe.


You can run this in GHCi, by the way,

Prelude> :{ 
Prelude| let x  = 1
Prelude|     x' = x * 2
Prelude| in x'
Prelude| :}
2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...