2.6 Conditional execution
if Conditional Execution Clause Keyword
unless Conditional Execution Clause Keyword
when
|if
} expr clause1 [and
clause]* [else
clause2 [and
clause]*]
unless
expr clause1 [and
clause]* [else
clause2 [and
clause]*]
when
andif
allow you to execute loop clauses conditionally. These constructs are synonyms and can be used interchangeably.
nil
, the expression clause1 is evaluated. If the test expression evaluates tonil
and anelse
construct is specified, the statements that follow theelse
are evaluated; otherwise, control passes to the next clause.
unless
construct is equivalent towhen (not expr)
andif (not expr)
. If the value of the test expression expr isnil
, the expression clause1 is evaluated. If the test expression evaluates to non-nil
and anelse
construct is specified, the statements that follow theelse
are evaluated; otherwise, control passes to the next clause.
and
to produce a compound clause.
it
to refer to the result of the test expression in a clause. If multiple clauses are connected withand
, theit
construct must be used in the first clause in the block. Sinceit
is a loop keyword, you cannot useit
as a local variable within a loop.
when
orif
clauses are nested, eachelse
is paired with the closest precedingwhen
orif
construct that has no associatedelse
.
;; Group conditional clauses into a block without using ;; BLOCK or PROGN. > (loop for i in numbers-list when (oddp i) do (print i) and collect i into odd-numbers and do (terpri) else ; I is even. collect i into even-numbers finally (return (values odd-numbers even-numbers)));; Collect numbers larger than 3. > (loop for i in '(1 2 3 4 5 6) when (and (> i 3) i) collect it) ; IT refers to (and (> i 3) i). (4 5 6)
;; Find a number in a list. > (loop for i in '(1 2 3 4 5 6) when (and (> i 3) i) return it) 4
;; The above example is similar to the following one. > (loop for i in '(1 2 3 4 5 6) thereis (and (> i 3) i)) 4
;; Nest conditional clauses. > (loop for i in list when (numberp i) when (bignump i) collect i into big-numbers else ; Not (bignump i) collect i into other-numbers else ; Not (numberp i) when (symbolp i) collect i into symbol-list else ; Not (symbolp i) (error "found a funny value in list ~S, value ~S~%" list i))
Generated with Harlequin WebMaker