4

In my functional programming class, the lecturer created the function reverse to take a list and return a list with the same elements but in reverse order:

reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

However, in the book I am learning from the define they create the same function with ' after the function's name:

reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]

I was wondering if these two functions behave in the same way, or if the ' alters their behavior? Furthermore, are there cases where one notation is better than the other? Any help would be appreciated!

K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71
Jengels
  • 440
  • 6
  • 15
  • 2
    You can think `'` suffix as a second version notation by a general convention among Haskell coders just like `reverse_v2`. In the book that you read there must have been a mention of the original `reverse` and `reverse'` is just indicating another (or the *next*) version showing how else it could be or is implemented – Redu Nov 21 '19 at 18:46
  • 2
    It doesn't "mean" anything, it's just a character that is legal to use in the names of values. It is indeed convention to use a "tick" at the end of a function name to denote a slightly modified version of a previously defined function, but this is nothing other than convention - the apostrophe character has no intrinsic meaning in the Haskell language. – Robin Zigmond Nov 21 '19 at 19:07
  • 4
    Almost: `'` cannot be the *first* character in name, as `Char` literals are indicated by a single character in single quotes. `'c'` – chepner Nov 21 '19 at 20:03

0 Answers0