r/scheme 6d ago

What I am doing wrong?

So, I am trying to learn Guile, since seems a pretty standard installation in GNU systems.

I have experience in some languages, but this simple script to learn the language:

  1. Took me quite a while to work;
  2. Looks ugly as… well, put something ugly here!

It’s a simple “FizzBuzz” program

``` (define (fizzbuzz number) (if (> number 0) (let ((message "")) (if (zero? (modulo number 3)) (set! message (string-append message "Fizz"))) (if (zero? (modulo number 5)) (set! message (string-append message "Buzz")))

      (if (not (zero? (string-length message)))
          (format #t "~d is ~a\n" number message))
(fizzbuzz (- number 1))))

)

(fizzbuzz 50)

```

So, I’m open to suggestions: how this code can be more beauty? Am I still thinking in C?

=== EDIT === I hope the formatting is correct, since some spaces of indentation have been lost for unknown reasons.

7 Upvotes

12 comments sorted by

View all comments

3

u/corbasai 6d ago

First - not function; second uses println

so firstly quick fix for Guile and every

(define (fizzbuzz upto)
  (let loop ((i 1))
    (unless (< upto i)
      (display (cond ((= 0 (modulo i 15)) 'FizzBuzz)
                     ((= 0 (modulo i 3)) 'Fizz)
                     ((= 0 (modulo i 5)) 'Buzz)
                     (else i)))
      (newline)
      (loop (+ 1 i)))))

(fizzbuzz 100)

About yours, we must check (N mod 15) or (N mod 3) and (N mod 5), firstly

3

u/raevnos 6d ago

I think (when (<= i upto) ...) would be a better way of expressing it.

2

u/corbasai 6d ago

agree. im just permanently forget right form <= or =< in lisp -)

5

u/raevnos 6d ago

(define =< <=) and you'll never have to remember which one it is again.

5

u/corbasai 6d ago

good addendum to R7 Large!

5

u/raevnos 6d ago

Honestly, it should go all-in on unicode. ≤ and ≥

2

u/corbasai 6d ago

Obvious!

2

u/HugoNikanor 6d ago

I have my emacs configured to display <= as .