Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created January 10, 2013 21:37
Show Gist options
  • Select an option

  • Save eraserhd/4506022 to your computer and use it in GitHub Desktop.

Select an option

Save eraserhd/4506022 to your computer and use it in GitHub Desktop.
FizzBuzz from the (fun (with Scheme)) open space at CodeMash
;; FizzBuzz
(define-macro (expect . args)
(if (= 2 (length args))
(let ((message (car args))
(test-expression (cadr args)))
`(if (not ,test-expression)
(raise ,message)))
`(if (not ,(car args))
(raise ',(car args)))))
(define (output-for number)
(define (divisible-by? divisor)
(= 0 (modulo number divisor)))
(define (fizzy?)
(divisible-by? 3))
(define (buzzy?)
(divisible-by? 5))
(define (meh?)
(and (not (fizzy?))
(not (buzzy?))))
(define (string-if condition string)
(if (condition)
string
""))
(string-append
(string-if fizzy? "Fizz")
(string-if buzzy? "Buzz")
(string-if meh? (number->string number))))
(expect (string=? "1" (output-for 1)))
(expect (string=? "2" (output-for 2)))
(expect (string=? "Fizz" (output-for 3)))
(expect (string=? "4" (output-for 4)))
(expect (string=? "Buzz" (output-for 5)))
(expect (string=? "Fizz" (output-for 6)))
(expect (string=? "Buzz" (output-for 10)))
(expect (string=? "FizzBuzz" (output-for 15)))
(let fizzbuzzer ((number 1))
(if (> number 100)
#f
(begin
(display (output-for number))
(newline)
(fizzbuzzer (+ 1 number)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment