Logo

Programming-Idioms

  • Lisp
  • C++
  • Haskell

Idiom #226 Delete last element from list

Remove the last element from the list items.

let items2 = init items

The init function is unsafe. It blows up if a list (or string) is empty.
pop :: [a] -> [a]
pop [] = []
pop xs = init xs

let items2 = pop items

This is safer than init.

pop [1,2,3] ~~> [1,2]
pop [] ~~> []
pop "abcd" ~~> "abc"
pop "" ~~> ""
items.pop_back();

Removes last element from items, without returning anything.

If items is already empty, then behaviour is undefined.
with Ada.Containers.Vectors;
Items.Delete_Last (Count => 1);

New implementation...
< >
programming-idioms.org