Skip to content

Instantly share code, notes, and snippets.

@wulab
Last active May 3, 2021 17:17
Show Gist options
  • Save wulab/f665ec2c7556d2ca183aa5eea41c8292 to your computer and use it in GitHub Desktop.
Save wulab/f665ec2c7556d2ca183aa5eea41c8292 to your computer and use it in GitHub Desktop.
Write a program that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz.
(define (fizzbuzz)
(define (fizzbuzz-iter counter max-count)
(print counter)
(if (>= counter max-count)
'done
(fizzbuzz-iter (+ counter 1) max-count)))
(define (print n)
(cond ((and (divisible n 3) (divisible n 5)) (write-line 'fizzbuzz))
((divisible n 3) (write-line 'fizz))
((divisible n 5) (write-line 'buzz))
(else (write-line n))))
(define (divisible a b)
(= (modulo a b) 0))
(fizzbuzz-iter 1 100))
(fizzbuzz)
@wulab
Copy link
Author

wulab commented May 3, 2021

$ mit-scheme --quiet < fizzbuzz.scm | column
1		16		31		46		61		76		91
2		17		32		47		62		77		92
fizz		fizz		fizz		fizz		fizz		fizz		fizz
4		19		34		49		64		79		94
buzz		buzz		buzz		buzz		buzz		buzz		buzz
fizz		fizz		fizz		fizz		fizz		fizz		fizz
7		22		37		52		67		82		97
8		23		38		53		68		83		98
fizz		fizz		fizz		fizz		fizz		fizz		fizz
buzz		buzz		buzz		buzz		buzz		buzz		buzz
11		26		41		56		71		86
fizz		fizz		fizz		fizz		fizz		fizz
13		28		43		58		73		88
14		29		44		59		74		89
fizzbuzz	fizzbuzz	fizzbuzz	fizzbuzz	fizzbuzz	fizzbuzz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment