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 '
?
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 ' 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.
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.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)
.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!