DEV Community


Posted on

Fizz Buzz with compile time programming

Fizz Buzz is screening problem for computer programmers. It is also used for esoteric programming play sometimes.

Kazuho wrote a program to generate a string of Fizz Buzz at compile time in C++. gos-k also wrote in the same concept in Common Lisp.

And I wrote in Scheme (R6RS).

(import (rnrs))

(define-syntax make-fizzbuzz-string
    (syntax-case ctx ()
      ((_ n)
       (integer? (syntax->datum #'n))
           (do ((i 1 (+ i 1)))
               ((< (syntax->datum #'n) i))
               ((zero? (mod i 15)) "fizzbuzz")
               ((zero? (mod i 5))  "buzz")
               ((zero? (mod i 3))  "fizz")
               (else i))
             (newline port))))))))

(define (fizzbuzz)
  (display (make-fizzbuzz-string 100)))

Enter fullscreen mode Exit fullscreen mode

For compile-time programming, LISP is much easier than C++.

Top comments (0)