6.5 mischief/boolean: Booleans and Conditionals
(require mischief/boolean) | package: mischief |
This module re-exports implies and xor from racket/bool.
syntax
(cond! cond-clause ...)
A variant of cond! that raises an exception if all clauses fail.
Examples:
> (define (sum xs) (cond! [(empty? xs) 0] [(cons? xs) (+ (first xs) (sum (rest xs)))])) > (sum (list 1 2 3)) 6
> (sum (vector 1 2 3)) eval:53.0: cond!: no clause succeeded
struct
(struct exn:fail:cond! exn:fail () #:transparent)
The exception type raised by cond! when all clauses fail.
Procedure versions of and, or, and implies. Since
they are procedures rather than macros, they do not short circuit but they can
be passed as values to higher-order functions.
Examples:
> (and? #true 1) 1
> (and? #false 1) #f
> (and? #false (error "no short-circuiting")) no short-circuiting
> (or? #false #false) #f
> (or? 1 #false) 1
> (or? 1 (error "no short-circuiting")) no short-circuiting
> (implies? 1 2) 2
> (implies? #false 2) #t
> (implies? #false (error "no short-circuiting")) no short-circuiting
Returns #true if x and y are either both true or
both false, and #false otherwise.
Equivalent to (not (xor a b)).
Examples:
procedure
(cons/optional x y) → any/c
x : any/c y : any/c
Examples:
> (cons/optional 1 '(2 3)) '(1 2 3)
> (cons/optional #false '(2 3)) '(2 3)
procedure
(list/optional x ...) → (listof (not/c #false))
x : any/c
Produces a list of each x that is not #false.
Example:
> (list/optional 1 #false 3 4 #false 6) '(1 3 4 6)
procedure
(list*/optional x ... y) → any/c
x : any/c y : any/c
Adds each x that is not #false to the (possibly improper)
list y.
Example:
> (list*/optional 1 #false 2 #false '(5 6)) '(1 2 5 6)