Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The meaning of ' in Haskell function name?

What is quote ' used for? I have read about curried functions and read two ways of defining the add function - curried and uncurried. The curried version...

myadd' :: Int -> Int -> Int
myadd' x y = x + y

...but it works equally well without the quote. So what is the point of the '?

like image 485
user147056 Avatar asked Jul 30 '09 16:07

user147056


People also ask

What are functions in Haskell?

Functions play a major role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its own functional definition and declaration. Function declaration consists of the function name and its argument list along with its output.

What does ++ mean in Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combines" them into a single list.

What is the type of the Haskell function?

Haskell has first-class functions : functions are values just like integers, lists, etc. They can be passed as arguments, assigned names, etc. … val is value of type Int , and half_of is a value of type Float -> Float .

What does apostrophe mean in Haskell?

That apostrophe doesn't have any special meaning in Haskell's syntax. It's a valid character to use in a function name. We usually use ' to either denote a strict version of a function (one that isn't lazy) or a slightly modified version of a function or a variable.


1 Answers

The quote means nothing to Haskell. It is just part of the name of that function.

People tend to use this for "internal" functions. If you have a function that sums a list by using an accumulator argument, your sum function will take two args. This is ugly, so you make a sum' function of two args, and a sum function of one arg like sum list = sum' 0 list.

Edit, perhaps I should just show the code:

sum' s [] = s
sum' s (x:xs) = sum' (s + x) xs

sum xs = sum' 0 xs

You do this so that sum' is tail-recursive, and so that the "public API" is nice looking.

like image 74
jrockway Avatar answered Sep 26 '22 03:09

jrockway