Intersecting Number Wheels
October 1, 2019
Our implementation follows the description exactly:
(define-generator (wheel xs) (let loop ((ws (cycle xs))) (if (integer? (car ws)) (yield (car ws)) (yield ((eval (car ws))))) (loop (cdr ws))))
Here are the examples from the Rosetta Code description:
> (define a (wheel '(1 2 3))) > (take-gen 20 a) (1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2) > (define a (wheel '(1 b 2))) > (define b (wheel '(3 4))) > (take-gen 20 a) (1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3) > (define a (wheel '(1 d d))) > (define d (wheel '(6 7 8))) > (take-gen 20 a) (1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6) > (define a (wheel '(1 b c))) > (define b (wheel '(3 4))) > (define c (wheel '(5 b))) > (take-gen 20 a) (1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4)
You can see the assembled program at https://ideone.com/lpMU0a, but it doesn’t run, I don’t know why, something to do with the define-generator
macro. I can assure you it runs fine in Chez Scheme on my systems at home and work.
Here is a very simple solution using only R7RS Scheme plus
circular-list from SRFI 1 (and unfold for a demo). It is avoids eval
but uses destructive updates. It also requires the wheels to be
defined without forward references, which could be considered a
feature that disallows wheels like A = (B) with B = (A).
Output:
Here’s a C++ solution, read the wheels from the command line, 1st arg is for A, 2nd for B etc. Store in array and use second array to store current offset for each wheel:
A better version:
Here’s a solution in Python.
Output: