Gauche Scheme:Say I want to create a function that fills a list of length n with a sequence that counts from 1 to n, at a given interval r and returns
the list. And this process being generalized for any given length or interval. Of course the length and interval have to be relatively
prime.
For example: If the interval is 3 and the length is 17, the list
returned would be...
(1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12)
You can see that, since the interval is 3, if you read every 3 in the
final list you will see 1 2 3 4...etc.
I have done this several different ways, but ran into some
limitations, and am now back to work on a better way. Any
suggestions? I have a feeling it is much simpler than I am making it.
Maybe?:
(let ((n 17)(i 3))
(loop with a = (make-array n)
for x below n
do (setf (svref a (mod (* i x) n)) (1+ x))
finally (return (coerce a 'list))))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 299 |
Nodes: | 16 (2 / 14) |
Uptime: | 28:45:31 |
Calls: | 6,681 |
Calls today: | 4 |
Files: | 12,222 |
Messages: | 5,342,516 |
Posted today: | 2 |