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)
ExpandDiskEdit
erlang
Result = init(List),
ExpandDiskEdit
erlang
Result = take(length(List) - 1, List),
ExpandDiskEdit
erlang
Result = lists:reverse(tl(lists:reverse(List))),

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