Login
|
Signup
langref.org
-
fantom
and
erlang
add..
all
clojure
cpp
csharp
fsharp
go
groovy
haskell
java
ocaml
perl
php
python
ruby
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Lists
Access
Fetch the last element of a list
Given the list
[Red, Green, Blue]
, access the last element (
'Blue'
)
fantom
["Red", "Green", "Blue"][-1]
class SolutionXX
{
Void main()
{
["Red", "Green", "Blue"][-1]
}
}
fantom
["One", "Two", "Three", "Four", "Five"].last
class SolutionXX
{
Void main()
{
["One", "Two", "Three", "Four", "Five"].last
}
}
erlang
Result = lists:last(List),
-module(last).
-export([start/0]).
start() ->
List = ['Red', 'Green', 'Blue'],
Result = lists:last(List),
io:format("~w~n", [Result]).
erlang
Result = last(List),
-module(last).
-export([start/0]).
start() ->
List = ['Red', 'Green', 'Blue'],
Result = last(List),
io:format("~w~n", [Result]).
last([]) -> throw(badarg);
last([H]) -> H;
last([_|T]) -> last(T).
erlang
Result = hd(lists:reverse(List)),
-module(last).
-export([start/0]).
start() ->
List = ['Red', 'Green', 'Blue'],
Result = hd(lists:reverse(List)),
io:format("~w~n", [Result]).
erlang
Result = lists:nth(length(List), List),
-module(last).
-export([start/0]).
start() ->
List = ['Red', 'Green', 'Blue'],
Result = lists:nth(length(List), List),
io:format("~w~n", [Result]).
Submit a new solution for
fantom
or
erlang
There are 25 other solutions in
additional
languages (
clojure
,
cpp
,
csharp
,
fsharp
...)