[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.3 srfi-2 - And-let*

Module: srfi-2
Provides SRFI-2 and-let* macro.

Macro: and-let* (binding ...) body ...
[SRFI-2] In short, it works like let*, but returns #f immediately whenever the expression in bindings evaluates to #f.

Each binding should be one of the following form:

(variable expression)
The expression is evaluated; if it yields true value, the value is bound to variable, then proceed to the next binding. If no more bindings, evaluates body .... If expression yieds #f, stops evaluation and returns #f from and-let*.
(expression)
In this form, variable is omitted. Expression is evaluated and the result is used just to determine whether we continue or stop further evaluation.

Let's see some examples. The following code searches key from an assoc-list alist and returns its value if found.
 
(and-let* ((entry (assoc key alist))) (cdr entry))

If arg is a string representation of an exact integer, returns its value; otherwise, returns 0:
 
(or (and-let* ((num (string->number arg))
               ((exact? num))
               ((integer? num)))
      num)
    0)

The following is a hypothetical code that searches a certain server port number from a few possibilities (environment variable, configuration file, ...)
 
(or (and-let* ((val (sys-getenv "SERVER_PORT")))
      (string->number val))
    (and-let* ((portfile (expand-path "~/.server_port"))
               ((file-exists? portfile))
               (val (call-with-input-string portfile port->string)))
      (string->number val))
    8080) ; default



This document was generated by Ken Dickey on November, 28 2002 using texi2html