11

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 '?

yairchu
  • 23,680
  • 7
  • 69
  • 109
user147056
  • 397
  • 1
  • 4
  • 10
  • 1
    Will you mind it renamed to "The meaning of ' in Haskell function name?" – EFraim Jul 30 '09 at 16:17
  • 1
    possible duplicate of [Apostrophe in identifiers in Haskell](http://stackoverflow.com/questions/5673916/apostrophe-in-identifiers-in-haskell) – Don Stewart Apr 17 '11 at 21:33

5 Answers5

23

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.

jrockway
  • 42,082
  • 9
  • 61
  • 86
  • 1
    `sum'` could also be in `sum`'s `where` clause. – mk12 Mar 22 '12 at 23:55
  • Beware accidentally calling the main function in your recursive function, i.e. `sum' s (x:xs) = sum (s + x) xs`. The type system would complain here, but if the functions have the same type then it's hard to find. – mk12 May 21 '12 at 21:26
  • It normally refers to a slightly altered version of the original function. – Maximilian Mordig Jul 12 '16 at 16:52
17

It is often pronounced "prime", so that would be "myadd prime". It is usually used to notate a next step in the computation, or an alternative.

So, you can say

add = blah
add' = different blah

Or

f x = 
  let x' = subcomputation x
  in blah.

It just a habit, like using int i as the index in a for loop for Java, C, etc.

Edit: This answer is hopefully more helpful now that I've added all the words, and code formatting. :) I keep on forgetting that this is not a WYSIWYG system!

agorenst
  • 2,425
  • 14
  • 18
9

There's no particular point to the ' character in this instance; it's just part of the identifier. In other words, myadd and myadd' are distinct, unrelated functions.

Conventionally though, the ' is used to denote some logical evaluation relationship. So, hypothetical function myadd and myadd' would be related such that myadd' could be derived from myadd. This is a convention derived from formal logic and proofs in academia (where Haskell has its roots). I should underscore that this is only a convention, Haskell does not enforce it.

Daniel Spiewak
  • 54,515
  • 14
  • 108
  • 120
6

quote ' is just another allowed character in Haskell names. It's often used to define variants of functions, in which case quote is pronounced 'prime'. Specifically, the Haskell libraries use quote-variants to show that the variant is strict. For example: foldl is lazy, foldl' is strict.

In this case, it looks like the quote is just used to separate the curried and uncurried variants.

Nathan Shively-Sanders
  • 18,329
  • 4
  • 46
  • 56
2

As said by others, the ' does not hold any meaning for Haskell itself. It is just a character, like the a letter or a number.

The ' is used to denote alternative versions of a function (in the case of foldl and foldl') or helper functions. Sometimes, you'll even see several ' on a function name. Adding a ' to the end of a function name is just much more concise than writing someFunctionHelper and someFunctionStrict.

The origin of this notation is in mathematics and physics, where, if you have a function f(x), its derivate is often denoted as f'(x).

mrueg
  • 8,185
  • 4
  • 44
  • 66