Return prematurely from function in Racket

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?

Upvotes: 9

Views: 846

Answers (1)

soegaard
soegaard

Reputation: 31147

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.

Upvotes: 10

Related Questions