On this page:
cond!
exn:  fail:  cond!
and?
or?
implies?
iff
cons/  optional
list/  optional
list*/  optional

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

(and? x ...)  any/c

  x : any/c

procedure

(or? x ...)  any/c

  x : any/c

procedure

(implies? x y)  any/c

  x : any/c
  y : any/c
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

procedure

(iff x y)  boolean?

  x : any/c
  y : any/c
Returns #true if x and y are either both true or both false, and #false otherwise. Equivalent to (not (xor a b)).

Examples:
> (iff 1 2)

#t

> (iff 1 #false)

#f

> (iff #false 2)

#f

> (iff #false #false)

#t

procedure

(cons/optional x y)  any/c

  x : any/c
  y : any/c
Returns y if x is #false, and (cons x y) otherwise.

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)