DEV Community

Discussion on: [Challenge] 🐝 FizzBuzz without if/else

Collapse
 
nombrekeff profile image
Keff • Edited

Thanks for sharing Tanguy!

Cool stuff! Haven't heard about r5rs, looks complicated, but cool :P

Collapse
 
tanguyandreani profile image
Tanguy Andreani • Edited

Oh it isn’t that hard, it might seem complicated because of all the parenthesis and strange keywords like cons, but it’s actually just a matter of getting used to it. It’s a fun language to learn recursion for example.

It becomes a lot simpler when you learn the patterns for building recursive functions, at one point, you don’t even look at the whole function because you just think of the parts.

I started by making a function that looks like that:

#lang r5rs

(define (fizzbuzz n acc)
  (define (compute n)
    n)
  (if (zero? n)
      acc
      (fizzbuzz (- n 1)
                (cons (compute n)
                      acc))))

(display (fizzbuzz 15 '()))
(newline)
; => (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)

This function just returns the list of numbers from 1 to 15, that's what it does because the compute function just returns whatever is n. Then I simply filled compute with my conditions.

  • define is used to define variables and functions,
  • cons is used to build a list from a head and a queue (that's not hard to understand but it definetely requires some practice and research,
  • let* allows to define local variables
Thread Thread
 
nombrekeff profile image
Keff • Edited

What a legend! I wasn't expecting an explanation, but it's highly appreciated!

Looks interesting, and after your explanation, it does not look as complicated, I might give it a try someday💪

And yeah, most languages seem a bit overwhelming at first.

Thread Thread
 
tanguyandreani profile image
Tanguy Andreani

Thanks! Feel free to ping me for help if you try to get into it and feel stuck.

Thread Thread
 
nombrekeff profile image
Keff

Thank you! I will if I get into it :)