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
  (lambda(ctx)
    (syntax-case ctx ()
      ((_ n)
       (integer? (syntax->datum #'n))
       (call-with-string-output-port
         (lambda(port)
           (do ((i 1 (+ i 1)))
               ((< (syntax->datum #'n) i))
             (display
              (cond
               ((zero? (mod i 15)) "fizzbuzz")
               ((zero? (mod i 5))  "buzz")
               ((zero? (mod i 3))  "fizz")
               (else i))
              port)
             (newline port))))))))

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

(fizzbuzz)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)