Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apostrophe in identifiers in Haskell

Tags:

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.

like image 365
kiltek Avatar asked Apr 15 '11 07:04

kiltek


People also ask

What does apostrophe do in Haskell?

It is indeed convention to use a "tick" at the end of a function name to denote a slightly modified version of a previously defined function, but this is nothing other than convention - the apostrophe character has no intrinsic meaning in the Haskell language.

What does single quote mean in Haskell?

Single quotes means single character, double quotes means character array (string). In Haskell, 'c' is a single character ( Char ), and "c" is a list of characters ( [Char] ).

Does Haskell pass by reference?

No, it's not possible, because Haskell variables are immutable, therefore, the creators of Haskell must have reasoned there's no point of passing a reference that cannot be changed.


2 Answers

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 
like image 69
Don Stewart Avatar answered Sep 23 '22 13:09

Don Stewart


It's just another character allowed in identifiers. Think of it as another letter.

like image 41
augustss Avatar answered Sep 24 '22 13:09

augustss