Source: The Axis of Eval: Common Lisp's BLOCK / RETURN-FROM and UNWIND-PROTECT
I was just chatting with Ben Titzer on Twitter about control flow in his Virgil language (which is cool and you should definitely check out), when I felt the need to once more promote how Common Lisp does non-local control flow (stuff like returning early from a function or breaking from a loop), because I think it's a very nice solution.
So in Common Lisp we have BLOCK / RETURN-FROM (which work as a pair) and UNWIND-PROTECT.
BLOCK / RETURN-FROM
BLOCK and RETURN-FROM effectively offer the same functionality as C's setjmp/longjmp -- non-local exits -- but nicely wrapped as we expect it in a lexically-scoped, expression-oriented language.
BLOCK / RETURN-FROM lets you do:
- Early returns from functions or arbitrary code blocks
- Breaking from loops, incuding any number of nested loops
- Continuing in loops, incuding any number of nested loops
- Even arbitrary GOTOs in a code block (with some macrology & trampolining, see Baker's TAGBODY)
(block name forms*) lexically binds name within the forms as a non-local exit from which you can return a value with (return-from name value). Just (return-from name) without a value uses nil as the value.
A BLOCK without any RETURN-FROM just returns the last value:
(block b 1 2 3) returns 3.
This prints 1 and returns 2:
(block b (print 1) (return-from b 2) (print 3))
You can have any number of nested blocks:
(block b1 ... (block b2 ... (return-from b1) ...) ...)
To do an early return from a function, place a block at its beginning:
(defun foo ()
(block b
...
(return-from b)
...))
(Common Lisp automatically places an anonymous block around every function body, so you don't need to do this in practice, but my hobby Lisp doesn't, and I'm using this explicit approach, and I like it.)
To break from a loop, place a block around it:
(block break
(loop
...
(return-from break)
...))
To continue in a loop, place a block inside it:
(loop
(block continue
...
(return-from continue)
...))
You can have multiple nested loops, like in Java:
(block break-outer
(loop
(block break-inner
(loop
...
(return-from break-inner)
...))))
UNWIND-PROTECT
UNWIND-PROTECT is effectively a try/finally block (without a catch).
Conclusion
Common Lisp's BLOCK / RETURN-FROM and UNWIND-PROTECT offer a minimalistic and expressive system for non-local control flow.
'Programming' 카테고리의 다른 글
(Racket) Collatz (0) | 2024.01.09 |
---|---|
백만 이하로 시작하는 우박수 중 가장 긴 과정을 거치는 것은? (0) | 2024.01.09 |
(Racket) 그래프 그리기 (plot) (0) | 2023.12.19 |
Code Complete, Second Edition eBook PDF (0) | 2023.11.11 |
(Racket) 매크로 정복하기 (1) | 2023.10.30 |