Changeset 29986 in project


Ignore:
Timestamp:
11/02/13 16:11:35 (8 years ago)
Author:
juergen
Message:

version 0.4 of anaphora with define-anaphor, define-properties, list-recurser, alist-recurser, tree-recurser and atree-recurser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/anaphora

    r25632 r29986  
    44== anaphora
    55
    6 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.
    7 
    8 Remember, that in a natural language an anaphor is an expression, which refers back in the conversation: "Buy this book and read it."
     6Inspired by Paul Graham's classic "On Lisp" this module introduces
     7anaphoric macros, which are unhygienic by design. Hence they can not be
     8implemented with syntax-rules! In fact, they introduce new identifiers
     9behind the scene, mostly named it, which can be referenced in the body
     10without being declared. All macros in this module start with an a prefix
     11to remind the user, that they pollute the namespace on purpose.
     12Corresponding operators without this prefix are well known.
     13
     14Remember, that in a natural language an anaphor is an expression, which
     15refers back in the conversation: "Buy this book and read it."
    916
    1017=== Programming interface
     
    1421<syntax>(anaphora sym)</syntax>
    1522
    16 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.
     23where sym is optional. Called without argument it returns the list of
     24exported symbols, with one of those symbols as argument it returns its
     25call-structure.
    1726
    1827==== aif
    1928
    20 <syntax>(aif test? consequent [alternative])</syntax>
     29<syntax>(aif test consequent [alternative])</syntax>
    2130
    2231Anaphoric version of if.
    23 
    24 Binds the result of test? to the symbol it, which can than be used in the consequent or the mandatory anternative.
     32Binds the result of test to the symbol it, which can than be used in the
     33consequent or the mandatory anternative.
    2534
    2635<enscript highlight=scheme>
     
    3039==== awhen
    3140
    32 <syntax>(awhen test? xpr . xprs)</syntax>
    33 
    34 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 ...
     41<syntax>(awhen test xpr . xprs)</syntax>
     42
     43Anaphoric version of when, i.e. the one-armed if, which allows multiple
     44expressions to be evaluated, if test succeeds. As with aif, the result
     45of test is stored in the variable it, which can be refered in xpr ...
    3546
    3647<enscript highlight=scheme>
     
    4253==== acond
    4354
    44 <syntax>(acond ((test? xpr ...) ... [(else xpr1 ...)]))</syntax>
     55<syntax>(acond ((test xpr ...) ... [(else xpr1 ...)]))</syntax>
    4556
    4657Anaphoric version of cond.
    47 
    48 The result of each test? is stored in the variable it, which can be used in the corresponding expressions xpr ...
     58The result of each test is stored in the variable it, which can be used
     59in the corresponding expressions xpr ...
    4960
    5061<enscript highlight=scheme>
     
    5566</enscript>
    5667
    57 Note, that the ordinary cond macro does something similar with the literal symbol =>.
     68Note, that the ordinary cond macro does something similar with the
     69literal symbol =>.
    5870
    5971==== awhile
    6072
    61 <syntax>(awhile test? xpr . xprs)</syntax>
     73<syntax>(awhile test xpr . xprs)</syntax>
    6274
    6375Anaphoric version of while.
    6476
    65 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.
     77The body xpr . xprs is evaluated as often, as the test is true. As
     78usual, the result of this test, which often is the result of a poll
     79operation, is named it and can be referenced in the body.
    6680
    6781<enscript highlight=scheme>
     
    8094
    8195Anaphoric version of and.
    82 
    83 When sequentially evaluating the arguments arg ..., the anaphor it will be bound to the value of the previous argument.
     96When sequentially evaluating the arguments arg ..., the anaphor it will
     97be bound to the value of the previous argument.
    8498
    8599<enscript highlight=scheme>
     
    94108Anaphoric version of lambda.
    95109
    96 The resulting procedure is bound to the anaphor self. This way, anonymous functions can be recursive as well.
     110The resulting procedure is bound to the anaphor self. This way,
     111anonymous functions can be recursive as well.
    97112
    98113<enscript highlight=scheme>
     
    101116</enscript>
    102117
     118==== define-anaphor
     119
     120<syntax>(define-anaphor name from rule)</syntax>
     121
     122Hygienic macro which writes an anaphoric macro with implicit it, name,
     123from another procedure or macro, from, with rule either #:cascade or
     124#:first
     125Note, that most of the macros above could have been created by means of
     126define-anaphor.
     127
     128<enscript highlight=scheme>
     129(define-anaphor alist list #:cascade)
     130(alist 1 (+ it 2) (* it 3)) ; -> '(1 3 9)
     131(define-anaphor awhen when #:first)
     132(awhen (* 1 2 3 4 5) (* 2 it)) ; -> 240
     133</enscript>
     134
     135==== define-properties
     136
     137<syntax>(define-properties name ...)</syntax>
     138
     139Abstracting away get and put!.
     140Defines for each name two macros, name and name!, the first being an
     141accessor to the property name, the second the corresponding mutator.
     142
     143<enscript highlight=scheme>
     144(define-properties color weight)
     145(color! 'foo 'red)
     146(color 'foo) ; -> 'red
     147(weight! 'foo 5)
     148(weight 'foo) ;-> 5
     149(color! 'foo 'blue)
     150(color 'foo) ; -> 'blue
     151(weight! 'foo 50)
     152(weight 'foo) ; -> 50
     153</enscript>
     154
     155==== list-recurser
     156
     157<procedure>(list-recurser recurser base)</procedure>
     158
     159generates a procedure which traverses on the cdrs of its only list
     160argument.
     161
     162<enscript highlight=scheme>
     163(define (lsome? ok?)
     164  (list-recurser (lambda (lst th) (or (ok? (car lst)) (th))) #f))
     165((lsome? odd?) '(2 4 5 6)) ; -> #t
     166</enscript>
     167
     168==== alist-recurser
     169
     170<syntax>(alist-recurser recur-xpr base-xpr)</syntax>
     171
     172anaphoric macro with internal symbols it, representing the current list,
     173and go-on, representing traversal along cdrs.
     174The result is a procedure, which recurs on its only list argument.
     175
     176<enscript highlight=scheme>
     177(define (alsome? ok?)
     178  (alist-recurser (or (ok? (car it)) (go-on)) #f))
     179((alsome? odd?) '(2 4 6)) ; -> #f
     180</enscript>
     181
     182==== tree-recurser
     183
     184<procedure>(tree-recurser recurser base)</procedure>
     185
     186generates a procedure which traverses on the cars and the cdrs of its
     187only tree argument.
     188
     189<enscript highlight=scheme>
     190(define tcopy
     191  (tree-recurser (lambda (tree left right)
     192                   (cons (left) (or (right) '())))
     193                 identity))
     194(tcopy '(1 (2 (3 4) 5) 6)) ; -> '(1 (2 (3 4) 5) 6)
     195</enscript>
     196
     197==== atree-recurser
     198
     199<syntax>(atree-recurser recur-xpr base-xpr)</syntax>
     200
     201anaphoric macro with internal symbols it, representing the current tree,
     202go-left and go-right, representing traversal along cars and cdrs
     203respectively.
     204The result is a procedure, which recurs on its only list argument.
     205
     206<enscript highlight=scheme>
     207(define atcopy
     208  (atree-recurser (cons (go-left) (or (go-right) '())) it))
     209(atcopy '(1 (2 3 (4)) 5)) ; -> '(1 (2 3 (4)) 5)
     210</enscript>
     211
    103212== Usage
    104213
     
    109218== Last update
    110219
    111 Sep 08, 2011
     220Nov 02, 2013
    112221
    113222== Author
     
    117226== License
    118227
    119  Copyright (c) 2011, Juergen Lorenz
     228 Copyright (c) 2011-2013, Juergen Lorenz
    120229 All rights reserved.
    121230
     
    148257== Version History
    149258
     259; 0.4 : added define-anaphor, define-properties, tree-recurser, atree-recurser, list-recurser and alist-recurser
    150260; 0.3 : helper-module anaphora-helper removed
    151261; 0.2 : documentation dispatcher added
Note: See TracChangeset for help on using the changeset viewer.