by Oleg Kiselyov
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.
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[ (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.
Last modified: Sun Jan 28 13:40:28 MET 2007