View Problem

Remove the last element of a list

DiskEdit
ruby
list = ['Apple', 'Banana', 'Carrot']
list.delete_at(-1)
DiskEdit
ruby
list = ['Apple', 'Banana', 'Carrot']
list.pop
DiskEdit
clojure
(pop ["Apple" "Banana" "Carrot"])
ExpandDiskEdit
erlang
Result = init(List),
ExpandDiskEdit
erlang
Result = take(length(List) - 1, List),
ExpandDiskEdit
erlang
Result = lists:reverse(tl(lists:reverse(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)

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