Search code examples
variablesscoperacketdo-loops

Confused with "do" variables in Racket code


Don't understand why "a" and "b" work in the code ? should we define var "a" and "b" before "do" ?

(define v1 3)
(define v2 2)
(do ((a 1 (+ a v1))
     (b 2 (+ b v2)))
  ((>= a b) (if (= a b) 'YES 'NO)))

Solution

  • After (do the local variables for the do loop are defined:

    • (a 1 (+ a v1)) meaning: define local loop variable a with starting value 1 and assigning (+ a v1) to a at the beginning of a new round
    • (b 2 (+ b v2)) meaning: define local loop variable b with starting value 2 and assigning (+ b v2) to b at the beginning of a new round

    So, a and b are defined in the do loop.