Sometimes I see code with the following expression:
example = example' []
Or what is the different between these functions?
foldl
foldl'
Sometimes I see code with the following expression:
example = example' []
Or what is the different between these functions?
foldl
foldl'
'
is simply another identifier character like any other. foldl'
is a completely separate function from foldl
; it could just as well be called strictFold
. It's called that because it's closely related to foldl
: it's a foldl
where the accumulator is evaluated at every step, so that large thunks don't build up. For instance, foldl (+) 0
will overflow the stack on a large list, but foldl' (+) 0
won't.
In general, the suffixing of a '
means one of three things: foo'
is either a helper definition made for the purpose of defining foo
, a modified version of foo
(that is, state
, state'
, and state''
could be an initial state and two updated versions), or a strict version of foo
.
Nothing, at least not in that context, since an apostrophe is a valid identifier character (see this answer for some more detail.)
It is a bit of a convention in the standard libraries that a strict version of certain functions is differentiated from the standard (lazy) version by an apostrophe, such as foldl'
(strict) and foldl
(lazy). This has nothing to do with the apostrophe being special though, it's just a convention.
In Haskell it is just another character to distinguish identifiers and the identifier is then called fold prime
, but it is commonly used in the same way as it used in mathematics.
There's a hackage module (I forget which one) that includes some kind of membership test (or some other predicate) in something. The test function is called "is":
foo x | x `is` whatever = ...
and for convenience the boolean negation of the function is also provided:
bar x | x `isn't` whatever = ...
I thought it was funny as heck when I first saw it. Apostrophe is just another character in the identifier.