Here is my list-based version. There are redundant calls to get the
length of the same list, but I didn't feel like factoring them out (call
it an exercise for the reader). The key to its simplicity is that
shifting an element is a similarity transform of shifting the first
element, with pre- a
Kevin,
Below is my attempt, which hopefully is bad enough to get this thread
rolling for you. :)
It rotates the 'i'th element 'n' times by swapping the 'i'th element with
the element to its right 'n' times. It looks horribly inefficient to me,
but is fairly simple and only depends on the prelud
On 火, 2007-6月-05, at 02:54, Greg Fitzgerald wrote:
> rotating the fourth element 2 positions would result in: [1, 2,
4, 3, 5]
Seems odd. Should that be [4,1,2,3,5]?
Yes, I meant to use the 5 element in my second example. Sorry for
the confusion.
> Is there an idomatic way to handle both
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:
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