KMCG

FizzBuzz Counts, Improved

Monday, July 25, 2016 A.D.

I improved the fizzbuzz solution in scheme from my previous post. I mentioned that if I added a second function inside of the main function, I could avoid passing in the n argument over and over again. I don't know if it's a change for the better but it's definitely.. uh… different. If nothing else, this code serves as a good lesson in why names are important.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(define fizzbuzz
  (lambda (m n col)
    (letrec
((F (lambda (m col)
    (cond
      ((> m n) (col 0 0 0))
      ((and (zero? (modulo m 5)) (zero? (modulo m 3)))
       (F (add1 m) (lambda (f b fb)
                      (col f b (add1 fb)))))
      ((zero? (modulo m 5)) (F (add1 m) (lambda (f b fb)
                                   (col f (add1 b) fb))))
      ((zero? (modulo m 3)) (F (add1 m) (lambda (f b fb)
                                   (col (add1 f) b fb))))
      (else (F (add1 m) (lambda (f b fb)
                           (col f b fb))))))))
      (F m col))))
                      
(fizzbuzz 5 15 (lambda (f b fb)
         (cons f (cons b (cons fb '())))))

It's essentially the same, but the letrec lets me simplify things somewhat. That's all.