0

When it comes to lists there are good, short and nice sounding names for most parts of list

  • Head
  • Tail
  • Last - somewhat commonly used

Currently I am doing a lots of TypeScript type manipulation and often I need to get all the starting elements of list/tuple except for the last element, e.g. part [1,2,3] of [1,2,3,4] and I don't really have good nice name for this, is there anything that is agreed upon for is less common case like in Head/Tail instance? I currently name it as Initial but don't really like it

1
  • 1
    To add to the language survey, Haskell has init function that returns all elements but last. Scala also has init method for iterables. Commented Jul 3 at 18:46

5 Answers 5

9

Smalltalk has names which I (as a Smalltalk programmer) consider pretty ok:

first and last return the first or last element of a sequenceable collection (array, list, etc).

allButFirst and allButLast return the sub-collection without the first or last element.

Many languages use .. (or :) to denote slices, and I find Python's solution most appealing. There, the indexes in a slice a[start:end], and start and end can be omitted or negative. All but first elements of a list then becomes a[1:], and all but last a[:-1], which is both terse and versatile and avoids the naming issue altogether.

4
  • 1
    this was also helpful and worth considering, thank you
    – Jarek
    Commented Jul 3 at 14:22
  • 3
    allButFirst So much better than cdr. Commented Jul 3 at 16:27
  • 1
    Javascript/Typescript also supports slicing with the Array.prototype.slice method using a similar convention to Python. e.g. [1, 2, 3, 4].slice(0, -1) would return [1, 2, 3].
    – Abion47
    Commented Jul 3 at 21:00
  • @candied_orange, agreed, it seems that the ButLast is an operator in the Logo programming language.
    – Steve
    Commented Jul 4 at 8:15
3

There is no particular standard or even informal standard for this, so you'll have to establish a term at least for your project.

To be understood as conveying a similar concept to those more well-known terms, it should be a short, common everyday word. "Body" unfortunately would imply leaving out the head and the tail, so it's out. "Start" (to me) sounds more like the meaning of "head", i.e. containing only one element. I think in your situation I'd go with front, which at least conveys a meaning of containing several elements rather than just one.

1
  • Front certainly is the best name I've heard so far
    – Jarek
    Commented Jul 3 at 12:08
3

Like @Killian I don't think there's an established word for this, when referring to something which is all-but-the-last.

Perhaps the "fore", or something derived from it as a prefix and strongly implying a multitude or an area (rather than a single item), like "foregroup" or "forepart". These are likely to be the most self-evident words if other people encounter them.

Or if you wanted to stay with the animal metaphors like head and tail, then perhaps "nose" or "beak", or even "snout".

The is also "trunk" - which besides being long and at the front of the elephant, has a strong connotation from trees of "the main thing" or "the major part".

I can't think of many objects which are regarded as having a long piece conventionally at the front as well as a short piece in the rear. Perhaps a whip, which has a "flail" and a handle - which would conveniently be a nice rhyming counterpart with "tail", and memorised as a "front tail".

0
0

In addition to the answers already given, you might consider prefix. This is imprecise, as a prefix could be any length, from nothing to the entire list, but in many contexts would be sufficient. It has the advantages of being short, well established both in math and computer science, and a word that reliably translates for any non-native English speakers reading the code.

0

In Haskell, it's called init

head   tail
 |   ___|____
 |  |        |
[1, 2, 3, 4, 5]
 |________|  |
     |       |
    init    last

The fp-ts library actually implements all 4 functions with the same names. Here's init.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.