View Problem

Remove the last element of a list

ExpandDiskEdit
fsharp
let take list n =
if n <= 0 then
list
else
let (left, _) = split_at list (n - 1)
left

// ------

let result = (take fruit ((List.length fruit) - 1))
ExpandDiskEdit
fsharp
let but_last list =
let rec but_last' list' acc =
match list' with
| [x] -> List.rev acc
| x :: xs -> but_last' xs (x :: acc)
if List.is_empty list then [] else but_last' list []

// ------

let result = (but_last fruit)
ExpandDiskEdit
fantom
list := ["Apple", "Banana", "Carrot"]
list.removeAt(-1)
ExpandDiskEdit
fantom
list := ["Apple", "Banana", "Carrot"]¨
list.pop
ExpandDiskEdit
groovy 1.0+
list = ['Apple', 'Banana', 'Carrot']
// to produce a new list
newlist = list[0,1]
// to modify original list
list.remove(2)

Submit a new solution for fsharp, fantom, or groovy
There are 19 other solutions in additional languages (clojure, cpp, csharp, erlang ...)