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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With