Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Haskell

Idiom #226 Delete last element from list

Remove the last element from the list items.

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 "" ~~> ""
let items2 = init items

The init function is unsafe. It blows up if a list (or string) is empty.
with Ada.Containers.Vectors;
Items.Delete_Last (Count => 1);

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