Search code examples
racketlist-comprehensionlexical-scope

Racket: lexical scope inside for


In Haskell, inside a list comprehension, i can bind expressions to a variable every iteration:

[a | x <- xs, let a = x ^ 2, a >= 25]

How do i bind lexical variables in Racket's for comprehension?

Currently i have this code:

(define (euler4)
  (apply max
         (for*/list ([i (in-range 100 1000)]
                     [j (in-range i 1000)]
                     #:when (string=? (number->string (* i j))
                                      (string-reverse (number->string (* i j)))))
           (* i j))))

I want to bound (* i j) to a variable and replace the expression to it everywhere in function.


Solution

  • Use the in-value form to have a loop variable that is bound to a single value.

    In your example:

    (define (euler4)
      (apply max
             (for*/list ([i (in-range 100 1000)]
                         [j (in-range i 1000)]
                         [ij (in-value (* i j))]
                         #:when (string=? (number->string ij)
                                          (string-reverse (number->string ij))))
               (* i j))))