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
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[ (BEGIN 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 ]]
The full implementation plus the validation code are available here (which is a copy of http://pobox.com/~oleg/ftp/Scheme/vland.scm).
This is an implementation of AND-LET* as a (Gambit) low-level macro that re-writes AND-LET* as a "tree" of AND and LET forms. A validation code is also presented, which verifies not only that everything works as expected, but also that AND-LET* finds syntax errors where expected.
This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Scheme Request For Implementation process or editors, except as needed for the purpose of developing SRFIs in which case the procedures for copyrights defined in the SRFI process must be followed, or as required to translate it into languages other than English.
The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.
This document and the information contained herein is provided on an "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Last modified: Wed Aug 13 10:42:11 CDT 2003