9

How do I return from a function before reaching the last form in standard Racket? That can be useful to avoid another level of indentation and form nesting.

In Common Lisp there is return, a specialized form of return-from. Any equivalent in Racket, at least for return only?

Greg Hendershott
  • 16,100
  • 6
  • 36
  • 53

1 Answers1

10

Use let/ec to create an escape continuation.

Example:

(let/ec return
  (for ([x 10000])
    (when (= x 100)
        (return x))))

Using let/ec is cheaper than using call/cc which creates a full continuation.

soegaard
  • 30,661
  • 4
  • 57
  • 106