Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 812
Antonio Bonifati 'Farmboy' Avatar asked Aug 27 '14 09:08

Antonio Bonifati 'Farmboy'


1 Answers

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.

like image 96
soegaard Avatar answered Oct 13 '22 21:10

soegaard