views:

354

answers:

5

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

+12  A: 

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
Oh I see.Thanks!!!
+7  A: 

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
+4  A: 

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 Sanders
+12  A: 

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!

Agor
+1  A: 

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