View Problem

Remove the last element of a list

DiskEdit
csharp
List<string> fruits = new List() { "apple", "banana", "cherry" };
fruits.RemoveAt(fruits.Length - 1);
ExpandDiskEdit
erlang
Result = init(List),
ExpandDiskEdit
erlang
Result = take(length(List) - 1, List),
ExpandDiskEdit
erlang
Result = lists:reverse(tl(lists:reverse(List))),
ExpandDiskEdit
cpp C++/CLI .NET 2.0
fruit->RemoveAt(fruit->Count - 1);
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
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 csharp, erlang, cpp, fsharp ...
There are 16 other solutions in additional languages (clojure, fantom, haskell, java ...)