Reputation: 65
I started with Haskell a few days ago and I found some solution for my problem online, but I started to notice that some function has a symbol at the end of the function name.
What is the meaning?
An example could be
map' :: (a -> b) -> [a] -> [b]
map' f [] = []
map' f (x:xs) = foldr (\y ys -> (f y):ys) [] xs
Upvotes: 5
Views: 308
Reputation: 105876
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
foldl'
),map'
in your own code, as it conflicts with Prelude.map
),x' = go x
) orEspecially 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.
Upvotes: 5
Reputation: 52270
The '
is usually added to indicate that this is some variant of a function map
. It's really just part of the name (it's a valid character).
In this example here map
is already provided by Prelude
so you'd have a name-conflict. So there is need for a different name and map'
does the job without thinking too much.
Often '
indicates that the function is strict (for example foldl'
) too.
Upvotes: 7