8.16.0.4
2 Conditional Expressions
(require scramble/cond) | package: scramble-lib |
syntax
(cond+ clause ... maybe-final-clause)
clause = [test-expr then-body ...+] | [test-expr => proc-expr] | #:do defn-or-expr maybe-final-clause =
| [else then-body ...+] | #:else then-body ...+
Like cond, but allows addititional forms of clauses and raises an error
if no clause is selected.
The additional forms of clause are allowed:
#:do defn-or-expr If the preceding clauses were not selected, evaluate defn-or-expr before continuing. If defn-or-expr is a definition, the subsequent clauses are in its scope.
Separate #:do clauses have “let*” scoping. That is, each #:do clause’s form is evaluated in a separate internal definition context nested within the previous scopes. Use begin to group mutually recursive definitions.
#:else then-body ...+ Allowed only at the end. Equivalent to [else then-body ...].
Examples:
> (define entries `([x 5] [y ,(lambda (key) (list key 12))]))
> (define (lookup key) (define entry (assoc key entries)) (cond+ [(not entry) (error "not found")] #:do (define v (cadr entry)) [(procedure? v) (v key)] #:else v)) > (lookup 'x) 5
> (lookup 'y) '(y 12)
> (lookup 'z) not found
> (cond+ [(odd? 12) 'odd] [(even? 7) 'even-odder]) cond+: all clauses failed
location: eval:7:0
syntax
(and+ part ... expr)
part = expr | #:do defn-or-expr
syntax
(or+ part ... expr)
part = expr | #:do defn-or-expr
Like and and or, respectively, but allowing #:do
clauses among the expressions. The parts must be followed by a final
expression— that is, unlike and and or, the arguments cannot
be empty.
Example: