Created
January 10, 2013 21:37
-
-
Save eraserhd/4506022 to your computer and use it in GitHub Desktop.
FizzBuzz from the (fun (with Scheme)) open space at CodeMash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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