View Problem

Remove the last element of a list

DiskEdit
clojure
(pop ["Apple" "Banana" "Carrot"])
ExpandDiskEdit
cpp C++/CLI .NET 2.0
fruit->RemoveAt(fruit->Count - 1);
ExpandDiskEdit
erlang
Result = init(List),
ExpandDiskEdit
erlang
Result = take(length(List) - 1, List),
ExpandDiskEdit
erlang
Result = lists:reverse(tl(lists:reverse(List))),
ExpandDiskEdit
fantom
list := ["Apple", "Banana", "Carrot"]
list.removeAt(-1)
ExpandDiskEdit
fantom
list := ["Apple", "Banana", "Carrot"]¨
list.pop
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)
DiskEdit
haskell
ages = [1,2,3,4]

init ages
DiskEdit
ocaml
let remove_last list =
match (List.rev list) with
| h::t -> List.rev t
| [] -> []
DiskEdit
ocaml
let remove_last lst =
List.rev (List.tl (List.rev lst))
DiskEdit
ocaml
let list_remove_last l =
let rec aux h q acc =
match q with
| [] -> List.rev acc
| h2 :: q -> aux h2 q (h :: acc) in
match l with
| [] -> invalid_arg "list_remove_last"
| h :: q -> aux h q []
DiskEdit
python
myList = ['Apple', 'Banana', 'Carrot']
myList.pop()

DiskEdit
ruby
list = ['Apple', 'Banana', 'Carrot']
list.delete_at(-1)
DiskEdit
ruby
list = ['Apple', 'Banana', 'Carrot']
list.pop
ExpandDiskEdit
scala
fruit = fruit.init
ExpandDiskEdit
scala
fruit = fruit.take(fruit.length - 1)

Submit a new solution for clojure, cpp, erlang, fantom ...
There are 5 other solutions in additional languages (csharp, java, perl, or php)