While trying to implement the Nautical Bell task on RosettaCode [1], I
needed an arithmetic way to express a rotation. The short story is clocks
on ships are origin-1, whereas modern clocks are origin zero (midnight is
00:00:00.000).
Now, if I had a list of all timestamps, the index change is simple: _1 |.
list_of_timestamps . The problem is, I'm given timestamps as scalars
(i.e. individual points in time), so I don't have a list. All I have is a
timestamp and a maximum (which corresponds to the length of the list).
Anyway, you can see the solution I cobbled together on the page, but what
I'm interested in is a general way to express rotations arithmetically
(i.e. as operations on indices). Is it true that after a rotation of R
places, the indices i of a list of length L will be given by L | (L+R) + i
?
Or, modelled in J:
rotate =: ((] | + + i.@]) #) { ]
_1 rotate i. 10
9 0 1 2 3 4 5 6 7 8
_1 rotate 'hello world!'
!hello world
2 rotate 'abcdefg'
cdefgab
(rotate"0 _~ i.@#) 'abcdefg'
abcdefg
bcdefga
cdefgab
defgabc
efgabcd
fgabcde
gabcdef
Can I rely on this identity for future work? Is there any scalar x and list
y where x rotate y will differ from x |. y ?
-Dan
[1] Nautical Bell task on RosettaCode
http://rosettacode.org/wiki/Nautical_bell#J
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm