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
python
l = ["apple", "orange", "grapes", "bananas"]
first, l = l[0], l[1:] + l[:1]
DiskEdit
python
fruit = ['apple', 'orange', 'grapes', 'bananas']
fruit.append(fruit.pop(0))
DiskEdit
clojure
(let [fruit ["apple" "orange" "grapes" "bananas"]]
(concat (rest fruit) [(first fruit)])
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)
ExpandDiskEdit
fantom
list := ["apple", "orange", "grapes", "bananas"]
list.add(list.removeAt(0))
ExpandDiskEdit
java 1.5 or later
list.add(list.remove(0));
ExpandDiskEdit
java
Collections.rotate(list, -1);
ExpandDiskEdit
cpp C++/CLI .NET 2.0
fruit->Add(fruit[0]); fruit->RemoveAt(0);
ExpandDiskEdit
cpp
rotate(fruit.begin(), fruit.begin()+1, fruit.end());

Submit a new solution for python, clojure, fsharp, fantom ...
There are 14 other solutions in additional languages (csharp, erlang, groovy, haskell ...)