Reputation:
I was wondering why in Haskell there are not symmetry in some function names:
For example:
head
: get the first element
last
: get the last element
Is there good reason why for example head
function was not named first
, or the other way around - last
function could be named end
or something similar.
Upvotes: 3
Views: 1396
Reputation: 476493
Haskell's function has two couples of list functions:
head :: [a] -> a
, and tail :: [a] -> [a]
; andinit :: [a] -> [a]
and last :: [a] -> a
.Learn You a Haskell for the Greater Good made a nice illustration about this:
The head
thus is the first element of the list, whereas the tail
is the list that contains the remaining elements.
The init
takes all the elements except the last one, and last
thus takes the last element.
A list in Haskell is a conceptually a linked list. Usually random access is not very common in list processing. Usually most list processing functions take a list and processes this like a stream of items.
It is common nomenclature of linked lists [wiki] to specify:
The
head
of a list is its first node. Thetail
of a list may refer either to the rest of the list after the head, or to the last node in the list. In Lisp and some derived languages, the next node may be called thecdr
(pronounced could-er) of the list, while the payload of the head node may be called thecar
.
Upvotes: 8