The character '
can be added to any identifier in Haskell, so map'
is an identifier. In this context, '
is also called "prime", so map'
would be pronounciated "map prime" if you were to read it out loud.
The use stems from mathematics, where function variants (often their derivatives) have some kind of symbol attached to them, e.g.
Function |
Meaning |
f |
Original function |
f' |
Derivative of f |
f'' |
Second derivative f |
f* |
Some special variant of f (usually called "f star") |
f̂ (f with ^) |
Some other special variant, usually the Fourier transform of f, called "f hat". |
In Haskell, the prime usually indicates either a
- strict variant (e.g.
foldl'
),
- a custom implementation (e.g.
map'
in your own code, as it conflicts with Prelude.map
),
- a value derived from another one (e.g.
x' = go x
) or
- an internal function (an implementation detail inside a library)
Especially the third variant can be found often in where
or let
clauses. Naming is hard after all, and a single prime allows you to both show the origin of the value and remove the need to come up with a better name:
-- | 'pow x n' calculates 'x' to the power of 'n'.
-- It uses the double-and-add method internally
pow :: Int -> Int -> Int
pow x 0 = 1
pow x 1 = x
pow x n
| even n = x' * x' -- usage of 'pow x (n/2)' here
| otherwise = x' * x' * x -- use of both x' and x
where
x' = pow x (n `div` 2) -- x' stems from the original x
Note that you may have arbitrary many '
in your identifier:
some'strange'example'don't'do'this :: Int
some'strange'example'don't'do'this = 12
foo'''''''''''''' = "please don't do this :)"
A single quote at the start of an identifier isn't allowed, as it would clash with a usual Char
, though.