by Oleg Kiselyov

This copy of the SRFI 2 specification document is distributed as part of the Racket package srfi-doc.

The canonical source of this document is https://srfi.schemers.org/srfi-2/srfi-2.html.

This SRFI is currently in *final* status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to `srfi-2@nospamsrfi.schemers.org`

. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

- Received: 1998-12-28
- Revised: 1999-02-09
- Draft: 1998-12-28--1999-02-28
- Final: 1999-03-01
- Revised to fix errata:
- 2020-06-08 (Fixed: Variables bound by AND-LET* needn't be unique. This requirement was based on the mistaken impression that LET* imposed that requirement, which it doesn't. Also, updated link to sample implementation to point to a more recent version by Oleg.)
- 2020-10-26 (Clarified what is meant by BODY in (and-let* (CLAWS) BODY).)

Like an ordinary AND, an AND-LET* special form evaluates its arguments -- expressions -- one after another in order, till the first one that yields #f. Unlike AND, however, a non-#f result of one expression can be bound to a fresh variable and used in the subsequent expressions. AND-LET* is a cross-breed between LET* and AND.

In case of an ordinary AND formed of proper boolean expressions:

(AND E1 E2 ...)

expression E2, if it gets to be evaluated, knows that E1 has returned
non-#f. Moreover, E2 knows exactly what the result of E1 was -- #t --
which E2 can use to its advantage. If E1 however is an extended
boolean expression, E2 can no longer tell which particular non-#f
value E1 has returned. Chances are it took a lot of work to evaluate
E1, and the produced result (a number, a vector, a string, etc) may be
of value to E2. Alas, the AND form merely checks that the result is
not an #f, and throws it away. If E2 needs it, it has to compute that
value anew. This proposed AND-LET* special form lets constituent
expressions get hold of the results of already evaluated expressions,
without re-doing their work.

AND-LET* can be thought of as a combination of LET* and AND, or a generalization of COND's send operator =>. An AND-LET* form can also be considered a sequence of guarded expressions. In a regular program, forms may produce results, bind them to variables and let other forms use these results. AND-LET* differs in that it checks to make sure that every produced result "makes sense" (that is, not an #f). The first "failure" triggers the guard and aborts the rest of the sequence (which presumably would not make any sense to execute anyway).

Examples:

(AND-LET* ((my-list (compute-list)) ((not (null? my-list)))) (do-something my-list)) (define (look-up key alist) (and-let* ((x (assq key alist))) (cdr x))) (or (and-let* ((c (read-char)) ((not (eof-object? c)))) (string-set! some-str i c) (set! i (+ 1 i))) (begin (do-process-eof))) ; A more realistic example ; Parse the 'timestamp' ::= 'token1' 'token2' ; token1 ::= 'YY' 'MM' 'J' ; token2 ::= 'GG' 'gg' "/" (define (parse-full-timestamp token1 token2) (AND-LET* (((= 5 (string-length token1))) ((= 5 (string-length token2))) (timestamp (OS:string->time "%m/%d/%y %H:%M" (string (string-ref token1 2) (string-ref token1 3) #\/ (string-ref token1 0) (string-ref token1 1) #\/ (case (string-ref token1 4) ((#\8 #\9) #\9) (else #\0)) (string-ref token1 4) #\space (string-ref token2 0) (string-ref token2 1) #\: (string-ref token2 2) (string-ref token2 3)))) ((positive? timestamp))) timestamp))

AND-LET* is also similar to an "anaphoric AND" LISP macro [Rob Warnock, comp.lang.scheme, 26 Feb 1998 09:06:43 GMT, Message-ID: 6d3bb3$3804h@fido.asd.sgi.com]. AND-LET* allows however more than one intermediate result, each of which continues to be bound through the rest of the form.

AND-LET* (CLAWS) BODY CLAWS ::= '() | (cons CLAW CLAWS) CLAW ::= (VARIABLE EXPRESSION) | (EXPRESSION) | BOUND-VARIABLE

- The CLAWS are evaluated in the strict left-to-right order
- For each CLAW, the EXPRESSION part is evaluated first (or BOUND-VARIABLE is looked up)
- If the result is #f, AND-LET* immediately returns #f
- Otherwise, if the CLAW is of the form (VARIABLE EXPRESSION) the EXPRESSION's value is bound to a freshly made VARIABLE
- The VARIABLE is available for the rest of the CLAWS , and the BODY
~~As usual, all VARIABLEs must be unique (like in let*)~~- BODY is a body as in a LET* form.

eval[ (AND-LET* (CLAW1 ...) BODY), env] = eval_claw[ CLAW1, env ] andalso eval[ (AND-LET* ( ...) BODY), ext_claw_env[CLAW1, env]] eval[ (AND-LET* (CLAW) ), env] = eval_claw[ CLAW, env ] eval[ (AND-LET* () FORM1 ...), env] = eval[ (LET* () FORM1 ...), env ] eval[ (AND-LET* () ), env] = #t eval_claw[ BOUND-VARIABLE, env ] = eval[ BOUND-VARIABLE, env ] eval_claw[ (EXPRESSION), env ] = eval[ EXPRESSION, env ] eval_claw[ (VARIABLE EXPRESSION), env ] = eval[ EXPRESSION, env ] ext_claw_env[ BOUND-VARIABLE, env ] = env ext_claw_env[ (EXPRESSION), env ] = env-after-eval[ EXPRESSION, env ] ext_claw_env[ (VARIABLE EXPRESSION), env ] = extend-env[ env-after-eval[ EXPRESSION, env ], VARIABLE boundto eval[ EXPRESSION, env ]]

A full sample implementation is available at http://okmij.org/ftp/Scheme/lib/myenv-chez.scm. The test suite is at http://okmij.org/ftp/Scheme/tests/vland.scm.

Copyright (C) Oleg Kiselyov (1998). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Editor: Dave Mason

Last modified: Sun Jan 28 13:40:28 MET 2007