DEV Community

Discussion on: Fizz Buzz in Every Language

Collapse
 
avalander profile image
Avalander • Edited

Here's my implementation in Racket

(define (mult-15? n)
  (and (mult-5? n)
       (mult-3? n)))

(define (mult-5? n)
  (= (modulo n 5) 0))

(define (mult-3? n)
  (= (modulo n 3) 0))

(define (fizzbuzz n)
  (cond
    [(mult-15? n) "FizzBuzz"]
    [(mult-5? n) "Buzz"]
    [(mult-3? n) "Fizz"]
    [else n]))

(define (print-list xs)
  (map displayln xs))

(print-list (map fizzbuzz (range 1 101)))

The print-list function is a bit redundant, since (map fizzbuzz (range 1 101)) will already print the resulting list to the console.

Collapse
 
gypsydave5 profile image
David Wickes

Ah come on @avalander - surely you should've written a FizzBuzz DSL in Racket? 😉

Collapse
 
avalander profile image
Avalander

I should, but I don't know enough Racket for that yet 😅

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Great stuff! I like racket a lot, but I haven't written any code in it myself. The fact that there are so many dialects of it is pretty cool to me.

Collapse
 
avalander profile image
Avalander

Thanks! I've just started learning it myself. I can recommend the book Realm of Racket if you want to give it a shot.