I would think a simple cyclic list should work without any copying at all:
rotateList myList n = take m . drop n $ x
where x = myList ++ x
m = length myList
Just keep dropping elements to rotate.
A possible alternative is to use a more tailored data structure with a
zipper. See http://www.haskell.org/haskellwiki/Zipper
Dan
kevin birch wrote:
Hello all,
I have an implementation question that I hope someone can help out
with. Say I have a fixed-size list: [1, 2, 3, 4, 5] that I want to
treat as circular in a function in order to rotate one of the elements n
positions. So rotating the second element 2 positions would result in:
[1, 3, 4, 2, 5], or rotating the fourth element 2 positions would result
in: [1, 2, 4, 3, 5]. There are two cases: if the element can be moved
w/o rotating and where the element must be inserted into the list at the
front. Is there an idomatic way to handle both of these cases in a
function?
Thanks,
kevin
------------------------------------------------------------------------
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe