1

I want to loop this one until the nr-of-bread is 2 how to do it?

(when (and (>= money price-of-bread) (< nr-of-bread 2))
        (set! nr-of-bread (+ nr-of-bread 1)) (set! money (- money price-of-bread)))
Hauleth
  • 22,873
  • 4
  • 61
  • 112
Tolga Yavuz
  • 159
  • 2
  • 10

1 Answers1

1

The best way to achieve that is using tail recursion via named let:

(let loop ((nr-of-bread 0)
           (rest money))
  (if (and (>= rest price-of-bread) (< nr-of-bread 2))
    (loop (add1 nr-of-bread) (- rest price-of-bread))
    nr-of-bread))
Hauleth
  • 22,873
  • 4
  • 61
  • 112