View Problem

Fetch the last element of a list

Given the list [Red, Green, Blue], access the last element ('Blue')
ExpandDiskEdit
fsharp
let last list =
let rec last' list' =
match list' with
| [x] -> x
| x :: xs -> last' xs
if List.is_empty list then failwith "empty list" else last' list

// ------

let result = last list
ExpandDiskEdit
fsharp
let result = (List.nth list ((List.length list) - 1))
ExpandDiskEdit
fsharp
let result = (List.hd (List.rev list))
DiskEdit
clojure
(last '[One Two Three Four Five])

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