guard
-- r7rs
Definition syntax
;
Syntax keywords:
variable
: identifier;else
: literal;clause-condition
: expression;clause-expression
: expression;clause
: pattern with variants:
(clause-condition)
;(clause-condition clause-expression |...|)
;(else clause-expression |...|)
;guarded-expression
: expression;Syntax variants:
(_ (variable clause |...|) guarded-expression |...|)
scheme:base
-- (scheme base)
;scheme
-- (scheme)
;(guard (<variable> <cond-clause_1> <cond-clause_2> ...) <body>)
Syntax: Each
<cond-clause>
is as in the specification ofcond
.Semantics: The
<body>
is evaluated with an exception handler that binds the raised object (seeraise
in section on exceptions) to<variable>
and, within the scope of that binding, evaluates the clauses as if they were the clauses of acond
expression. That implicitcond
expression is evaluated with the continuation and dynamic environment of theguard
expression. If every<cond-clause>
's<test>
evaluates to#f
and there is no else clause, thenraise-continuable
is invoked on the raised object within the dynamic environment of the original call toraise
orraise-continuable
, except that the current exception handler is that of theguard
expression.See section on exceptions for a more complete discussion of exceptions.
(guard (condition ((assq 'a condition) => cdr) ((assq 'b condition))) (raise (list (cons 'a 42)))) ===> 42 (guard (condition ((assq 'a condition) => cdr) ((assq 'b condition))) (raise (list (cons 'b 23)))) ===> (b . 23)
The text herein was sourced and adapted as described in the "R7RS attribution of various text snippets" appendix.