All Manuals > LispWorks® User Guide and Reference Manual > 38 The LISPWORKS Package

when-let

when-let*

if-let Macros

Summary

Executes a body of code if a form or series of forms evaluate to non-nil, making the result of the form(s) available in the body of code.

Package

lispworks

Signatures

when-let (var form) &body body => result*

when-let* bindings &body body => result*

if-let (var form) then-form &optional else-form => result*

bindings ::= ((var form)*)

Arguments
var
A symbol.
form
A form.
body
A body of code to be evaluated conditionally on the result of form.
then-form, else-form
Forms.
Values
result*
The results of evaluating body, then-form or else-form (see below).
Description

The macro when-let first evaluates form. If form returns non-nil, then var is bound to this value, the forms of body are evaluated sequentially and the values of the final form of body are returned. Otherwise nil is returned.

The macro when-let* expands into nested when-let forms. The bindings are evaluated in turn as long as each form returns non-nil. If the last form also evaluates to non-nil, the forms of body are evaluated sequentially. Each variable var is bound to the result of the corresponding form form while evaluating the next binding and all variables are bound while evaluating body. If body is evaluated then the values of its final form are returned. Otherwise nil is returned.

The macro if-let first evaluates form. If form returns non-nil then var is bound to the value of form and the values returned by evaluating then-form are returned. Otherwise the values of returned by evaluating else-form are returned.

Examples

The form:

(when-let (position (search string1 string2))
   (print position))

is equivalent to:

(let ((position (search string1 string2)))
   (when position
      (print position)))

This example uses the when-let* macro:

(defmacro divisible (n &rest divisors) 
  `(when-let* ,(loop for div in divisors 
                     collect (list (gensym) 
                                   (zerop (mod n div)))) 
     t))
See also

when
if
let
let*


LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:41