View Problem

Rotate a list

Given a list ["apple", "orange", "grapes", "bananas"], rotate it by removing the first item and placing it on the end to yield ["orange", "grapes", "bananas", "apple"]
DiskEdit
ruby
items = ["apple", "orange", "grapes", "bananas"]
items << first = items.shift

# items is rotated
# first contains the first value in the list
ExpandDiskEdit
erlang
N = 1, {Left, Right} = lists:split(N, List), Result = Right ++ Left,
ExpandDiskEdit
erlang
N = 1, Result = rotate(N, List),
ExpandDiskEdit
csharp 2.0 (LinkedList), 3.0 ("var" keyword)
var lst = new LinkedList<String>(new String[] {"apple", "orange", "grapes", "banana"});
lst.AddLast(lst.First());
lst.DeleteFirst();
ExpandDiskEdit
fsharp
let rotate list n =
if n <= 0 then
list
else
let (left, right) = split_at list (n - 1)
right @ left

// ------

let result = (rotate fruit 1)

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