Return prematurely from function in Racket
Asked Answered
B

1

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?

Biblicist answered 27/8, 2014 at 9:28 Comment(0)
B
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.

Broderick answered 27/8, 2014 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.