wheel-utils/example.scm
2020-06-04 14:49:02 +02:00

43 lines
1.2 KiB
Scheme

(use-modules (srfi srfi-1)
(wheel-utils wheel))
;;; Example: fizzbuzz.
;; First we generate out sub-proto-wheels.
(define fizz (every-n-in "fizz" 3))
(define buzz (every-n-in "buzz" 5))
;; Then we merge our proto-wheels
(define proto-wheel (generate-wheel
(lambda x
(let ((l (filter values x)))
(if (null? l)
#f
(apply string-append l))))
fizz
buzz))
;; Then we make our wheel circular so that we don't have to check any null.
(define actual-wheel (apply circular-list proto-wheel))
;; Here we loop from 1 to 99 and print every "opening" in the wheel
;; This _should_ be quite a lot faster than a solution that uses
;; division, and it should be quite extensible. We could easily add
;; a baz every 7 numbers.
(define (wheel-fizzbuzz n)
(let loop ((i 1) (wheel actual-wheel))
(if (> i n)
'()
(cons (or (car wheel) i) (loop (+ i 1) (cdr wheel))))))
(define (regular-fizzbuzz n)
(let loop ((i 1))
(cond
((> i n) '())
((eq? 0 (euclidean-remainder i 15))
(cons "fizzbuzz" (loop (+ i 1))))
((eq? 0 (euclidean-remainder i 3))
(cons "fizz" (loop (+ i 1))))
((eq? 0 (euclidean-remainder i 5))
(cons "buzz" (loop (+ i 1))))
(else
(cons i (loop (+ i 1)))))))