Question

I'm new to Haskell and what really bothers me at the moment is why the people who made the libraries added functions called fst and snd instead of using (normal) names like first and second. What's wrong with the latter names?

Personally I find these abbreviations ridiculous: "Why snd? Why not sec, scnd or scd? Or why not fir, frst, ft or frt?" It just lacks style. (But again - that's a personal opinion)

Was it helpful?

Solution

It's clear the names were chosen to be 3 characters long whilst being as clear as possible as to the word - miss out all but the most prominent consonants. A lot of the standard functions have better/easier-to-follow names, but admittedly not all!

It's historical accident, really, what the names were, and there's not a lot we can do about it without annoying a lot of people who have got used to them and have a large code base relying on them. At least Haskell didn't call search grep!

Feel free to write first = fst and second = snd at the top of your file - it won't take long to use your preferred name, and ghc -O2 will compile them out.

first and second are actually used for something more complicated in Control.Arrow but you don't need to go that deep at this stage.

OTHER TIPS

This is just speculation but I'll give it a shot. Both those are used in very specific cases. They only access the first and second elements of a tuple that is only two elements. They don't work for other sizes of tuples. In many cases a programmer may have a type that has two elements but instead of using a tuple they will make their own class because it will be more descriptive.

Also, since they both are only used on a 2 element tuple it may frequently happen that you write fst when you mean snd and vice a versa. Both these abbreviations are are 3 characters so when you go back and edit your source after you realize your mistake it won't change the format of your code or shift any characters in that line. Not that important but still nice.

Opposed to object oriented languages a lot of code in haskell involves chaining together functions in one line is it kind of nice to have short functions so that it all fits in one line. Many programmers use the 79 characters per line and there certainly is something appealing about not having to have a new line in one of your functions because it is too long. Especially, a function that uses guards or pattern matching.

To show how terse haskell can be and still have good style.

sum []     = 0
sum (x:xs) = x + sum xs

Opposed to C++

template <T>
T sum(list<T> lst)
{
   T s = 0;
   for(list<T>::iterator it=lst.begin();it!=lst.end();++it)
      s += *it;

   return s;
}

But this is just a silly example.

I think it is a lot prettier than some object oriented code that has a 150 character line.

One advantage with snd and fst is that they are equally long and code aligns nicely. Say

fun :: Bool -> (a, a) -> a

Now,

fun True  pair = fst pair
fun False pair = snd pair

Compared to

fun True  pair = first pair
fun False pair = second pair

The word pair is not aligned and I can't change them both simultaneously using visual mode in vim. It also bothers me visually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top