Hacker News new | ask | show | jobs
by _a_a_a_ 1148 days ago
Neither did goto in Pascal. Exceptions are further constrained. You can't do

10: goto 10

With exceptions.

> since we do not write imperative code nowadays

Who's 'we'? I certainly do when appropriate.

1 comments

In fact you can. In Common Lisp, (go ...) is a dynamic control transfer that performs unwinding and can jump out of a lambda. This is similar to what blub languages call exception handling.

  (tagbody
  10 
    (format t "label 10~%")
    (unwind-protect
      (funcall (lambda () (go 10)))
      (format t "unwinding!~%")))
Output:

  label 10
  unwinding!
  label 10
  unwinding!
  ...
How it works is that (go 10) identifies the surrounding tagbody as an exit point for the exception-like dynamic control transfer. That (go 10) is occurring in a sub-form of tagbody. That entire sub-form is abandoned, with unwinding, and then tagbody catches that control transfer, like an exception handler. tagbody then switches control to the desired label. Effectively, the exception-like control transfer is accompanied by a piece of datum: the label.

tagbody requires lexical visiblity between the go and tagbody; though the label bindings and the control transfer are dynamic, they have to be physically enclosed. That allows compilers to optimize tagbody more. A Common lisp compiler doesn't have to suspect that function (foo) can branch to the label bar in (tagbody (foo) bar) and can conclude that the label is unused, and the tagbody can be entirely optimized away. However, cross-function go does work, as the lambda shows in my example.

is this something to do with call/cc? (which I never understood)