Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ' mean/do at the end of function name in Haskell?

Tags:

syntax

haskell

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

Source

like image 526
macros Avatar asked Apr 22 '21 10:04

macros


People also ask

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What does apostrophe mean in Haskell?

This answer is not useful. Show activity on this post. 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. Follow this answer to receive notifications.

What do brackets mean in Haskell?

Haskell uses parentheses only in the way they are used in high school mathematics: for grouping sub-expressions so that you get a different call structure. For example, in maths 1 + 2 * 3 would call * on 2 and 3, and call + on 1 and the result of the * call.

What is at symbol in Haskell?

The @ Symbol is used to both give a name to a parameter and match that parameter against a pattern that follows the @ . It's not specific to lists and can also be used with other data structures.


2 Answers

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.

like image 73
Random Dev Avatar answered Nov 11 '22 14:11

Random Dev


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 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.

like image 38
Zeta Avatar answered Nov 11 '22 14:11

Zeta