Inspired by Paul Graham's classic "On Lisp" this module introduces anaphoric macros, which are unhygienic by design. Hence they can not be implemented with syntax-rules! In fact, they introduce new identifiers behind the scene, mostly named it, which can be referenced in the body without being declared. All macros in this module start with an a prefix to remind the user, that they pollute the namespace on purpose. Corresponding operators without this prefix are well known.
Remember, that in a natural language an anaphor is an expression, which refers back in the conversation: "Buy this book and read it."
- (anaphora sym) syntax
where sym is optional. Called without argument it returns the list of exported symbols, with one of those symbols as argument it returns its documentation.
- (aif test? consequent [alternative]) syntax
Anaphoric version of if.
Binds the result of test? to the symbol it, which can than be used in the consequent or the mandatory anternative.
(aif (memv 3 '(1 2 3 4 5)) it #f)
- (awhen test? xpr . xprs) syntax
Anaphoric version of when, i.e. the one-armed if, which allows multiple expressions to be evaluated, if test? succeeds. As with aif, the result of test? is stored in the variable it, which can be refered in xpr ...
(awhen (memv 3 '(1 2 3 4 5)) (print it) (reverse it))
- (acond ((test? xpr ...) ... [(else xpr1 ...)])) syntax
Anaphoric version of cond.
The result of each test? is stored in the variable it, which can be used in the corresponding expressions xpr ...
(acond ((memv 6 '(1 2 3 4 5)) it) ((memv 3 '(1 2 3 4 5)) it) (else it))
Note, that the ordinary cond macro does something similar with the literal symbol =>.
- (awhile test? xpr . xprs) syntax
Anaphoric version of while.
The body xpr . xprs is evaluated as often, as the test? is true. As usual, the result of this test?, which often is the result of a poll operation, is named it and can be referenced in the body.
(let ((lst '(1 2 3 4 5 #f)) (res '())) (awhile (car lst) (set! res (cons (car lst) res)) (set! lst (cdr lst))) res)
Of course, this is not the preferred programming style in Scheme ...
- (aand arg ...) syntax
Anaphoric version of and.
When sequentially evaluating the arguments arg ..., the anaphor it will be bound to the value of the previous argument.
(let ((lst '(1 2 3))) (aand lst (cdr it) (cdr it)))
- (alambda args xpr . xprs) syntax
Anaphoric version of lambda.
The resulting procedure is bound to the anaphor self. This way, anonymous functions can be recursive as well.
(map (alambda (n) (if (zero? n) 1 (* n (self (- n 1))))) '(1 2 3 4 5))
Sep 08, 2011
Copyright (c) 2011, Juergen Lorenz All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- helper-module anaphora-helper removed
- documentation dispatcher added
- initial import