Arc's simplest mechanism for non-linear control flow is catch and throw. This allows execution to jump back to the catch expression, for example to exit a loop early.
arc> (catch (each x '(2 4 6 9 10 12) (prn "Examining " x) (if (odd x) (throw x)))) Examining 2 Examining 4 Examining 6 Examining 9 9
Arc also provides access to MzScheme's exception mechanism, which provides a way to trap errors or raise error.
The ccc function is equivalent to Scheme's call-with-current-continuation (often abbreviated as call/cc). Continuations are extremely powerful; for details
see Teach Yourself Scheme in Fixnum Days, Chapter 20 of "On Lisp",
Call with Current Continuation Patterns, or Advanced Scheme Techniques. In MzScheme, continuations are relatively expensive if the call stack is deep since capturing a continuation copies the stack.
name and executes body. If name is
executed within body, that value will be returned from
point. Otherwise, point has no effect and the value from body will be
returned.
>(+ 10 (point throw (+ 20 (throw 3) 30)))
13
body and catches any value passed to throw. This is the same as point with the throw function's name predefined.
>(+ 10 (catch (+ 20 (throw 3) 30)))
13
call/cc or call-with-current-continuation.
>(ccc (fn (ep) (ep "bailout value") 42))
"bailout value"
after-procedure is executed whenever during-procedure exits.
>(protect (fn () (/ 1 0))
(fn () (prn "after")))
after
Error: /: division by zero
after-body executes whenever during-body exits.
>(on-err (fn (ex) (details ex)) (err "boo"))
Error: boo
>(err "Failure" 42)
Error: Failure 42
>(on-err (fn (ex) (string "caught " (details ex)))
(fn () (/ 1 0)))
"caught /: division by zero"
>(errsafe (/ 1 2))
1/2
>(errsafe (/ 1 0))
nil
Copyright 2008 Ken Shirriff.