Changeset 29965 in project


Ignore:
Timestamp:
10/26/13 14:13:33 (8 years ago)
Author:
juergen
Message:

list-bindings 1.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/list-bindings

    r29859 r29965  
    140140exectues body in this context.
    141141
     142==== macro-rules
     143
     144<syntax>(macro-rules sym ... () (pat0 tpl0) (pat1 tpl1) ...)</syntax>
     145<syntax>(macro-rules sym ... (suffix suffixed-keyword ...) (pat0 tpl0) (pat1 tpl1) ...)</syntax>
     146
     147This macro is inspired by syntax-rules and is used like it (after issuing
     148import-for-syntax). The differences to syntax-rules are the following:
     149
     150First, injected - hence unhygienic - symbols sym ... before the keyword
     151list are allowed. So anaphoric macros are possible and clearly
     152documented.
     153
     154Second, if the keyword-list is nonempty, it must be of the form
     155
     156  (? keyword? ...)
     157
     158which means that keyword? is a predicate which checks for its own name
     159with the suffix ? stripped (other common suffixes are allowed but
     160unusual).
     161
     162Third, the patterns pat0 pat1 ... are nested lambda-lists, i.e. ellipses
     163have no special meaning, if used at all.
     164
     165Third, the templates tpl0 tpl1 ... evaluate usually to quasiquoted
     166expressions as is the case in most low-level macros.
     167
    142168=== Requirements
    143169
     
    149175
    150176(use list-bindings)
     177(import-for-syntax (only list-bindings macro-rules))
    151178
    152179(bind a 1 a) ; -> 1
     
    252279  ; -> '(3 5)
    253280
     281(define-syntax aif
     282        (macro-rules it ()
     283                ((_ test consequent . alternative)
     284                 (if (null? alternative)
     285                        `(let ((,it ,test))
     286                                 (if ,it ,consequent))
     287                        `(let ((,it ,test))
     288                                 (if ,it ,consequent ,(car alternative)))))))
     289(define (mist x) (aif (! x) it))
     290(mist 5) ; -> 120
     291
     292(define-syntax if-then-
     293        (macro-rules (? then? else?)
     294                ((_ test then-pair)
     295                 (if (and (pair? then-pair) (then? (car then-pair)))
     296                         `(if ,test
     297                                        (begin ,@(cdr then-pair)))
     298                         `(error 'if-then- "syntax-error")))
     299                ((_ test then-pair else-pair)
     300                 (if (and (pair? then-pair) (then? (car then-pair))
     301                                                        (pair? else-pair) (else? (car else-pair)))
     302                         `(if ,test
     303                                        (begin ,@(cdr then-pair))
     304                                        (begin ,@(cdr else-pair)))
     305                         `(error 'if-then- "syntax-error")))))
     306(define (quux x)
     307        (if-then- (odd? x) (then "odd") (else "even")))
     308(quux 3) ; -> "odd"
     309(quux 4) ; -> "even"
     310
    254311</enscript>
    255312
    256313== Last update
    257314
    258 Oct 06, 2013
     315Oct 26, 2013
    259316
    260317== Author
     
    295352== Version History
    296353
     354; 1.7 : macro-rules added
    297355; 1.6 : bind-lambda and bind-case-lambda added
    298356; 1.5 : bind-set! and bind-define added, code partially rewritten
Note: See TracChangeset for help on using the changeset viewer.