如何在Racket中循环?

时间:2015-11-07 12:41:41

标签: racket

我想循环这个,直到nr-of-bread是2怎么做?

(when (and (>= money price-of-bread) (< nr-of-bread 2))
        (set! nr-of-bread (+ nr-of-bread 1)) (set! money (- money price-of-bread)))

1 个答案:

答案 0 :(得分:1)

实现这一目标的最佳方法是使用名为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))