ITT: We write FizzBuzz in any of our favorite languages. General discussion behind showcased languages is encouraged.
Forth: : foo? DUP 3 MOD 0 = IF ." Fizz" THEN ;
: bar? DUP 5 MOD 0 = IF ." Buzz" THEN ;
: neither? DUP 3 MOD 0 IF DUP 5 MOD 0 IF DUP . THEN THEN CR ;
: fizzBuzz! DUP 0 IF foo? bar? neither? 1- RECURSE THEN ;
100 fizzBuzz!
Attached: johnny_automatic_bending_bee_from_side.png (267x300, 33K)
June 18, 2019 - 17:17
Go: package fizzbuzz import "fmt" func Fire(ceiling uint) { var iterator uint for ; ceiling > iterator; iterator++ { if iterator % 3 == 0 { fmt.Printf("Fizz") } if iterator % 5 == 0 { fmt.Printf("Buzz") } if iterator % 3 != 0 && iterator % 5 != 0 { fmt.Printf("%d", iterator) } fmt.Printf("\n") } }
June 18, 2019 - 17:24
if number = divisible by 3 print fizz if number = divisible by 5 print buzz number = number + 1
June 18, 2019 - 18:43
>if iterator % 3 != 0 && iterator % 5 != 0 this should be the first condition in your loop, since the loop will be read sequentially
June 18, 2019 - 19:03
```lisp (defun f() (defvar l) (setq l 1) (loop (FizzBuzz l) (setq l (+ l 1)) (when (> l 100) (return))))
(defun FizzBuzz(n) (cond ((and (= (mod n 3) 0) (= (mod n 5) 0)) (write-line "FizzBuzz")) ((= (mod n 3) 0) (write-line "Fizz")) ((= (mod n 5) 0) (write-line "Buzz")) (t (format t "~a~%" n)))) ```
June 18, 2019 - 20:57
Rust
for i in 0..1000 { match (i % 3, i % 5) { (0, 0) => println!("FizzBuzz"), (0, _) => println!("Fizz"), (_, 0) => println!("Buzz"), (_, _) => println!("{}", i) } }
June 18, 2019 - 21:10
why? That's not his condition to print fizzbuzz
June 18, 2019 - 21:16
Common Lisp:
(defmacro fizzbuzz (max &body cases) `(loop for i from 1 to ,max do (let ((output "")) ,@(loop for (divisor text) in cases collecting `(when (= 0 (mod i ,divisor)) (setf output (concatenate 'string output ,text)))) (if (string-equal output "") (setf output i)) (format t "~A~%" output))))
(fizzbuzz 100 (3 "Fizz") (5 "Buzz") (7 "Fluzz"))
June 18, 2019 - 21:17
Common Lisp (loop for n from 1 to 100 do (format t "~&~[~[FizzBuzz~:;Fizz~]~*~:;~[Buzz~*~:;~D~]~]~%" (mod n 3) (mod n 5) n))
June 18, 2019 - 21:19
Datalog:
.decl InRange(n: number) InRange(1). InRange(n+1) :- InRange(n), n < 100.
.decl FizzBuzz(n: number, s: symbol) FizzBuzz(n, "FizzBuzz") :- InRange(n), n%3 = 0, n%5 = 0. FizzBuzz(n, "Fizz") :- InRange(n), n%3 = 0, n%5 != 0. FizzBuzz(n, "Buzz") :- InRange(n), n%3 != 0, n%5 = 0. FizzBuzz(n, to_string(n)) :- InRange(n), n%3 != 0, n%5 != 0.
// This last relation is necessary so that the output is sorted by number instead of by string .decl OrderedPairs(n: number, s: symbol) OrderedPairs(n, s) :- InRange(n), FizzBuzz(n, s). .output OrderedPairs(IO=stdout)
June 18, 2019 - 21:28