Changeset 38806 in project


Ignore:
Timestamp:
07/27/20 17:29:41 (13 days ago)
Author:
juergen
Message:

bindings 4.0 docu with dotted patterns

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/bindings

    r38643 r38806  
    1818types can be nested and mixed as you need it. This flexibility is made
    1919possible by one generic function, bind-listify*, which can add three
    20 procedures, seq? seq-car and seq-cdr to its local database and thus
    21 supplying support for a new sequence type.
     20(or four) procedures, seq?, seq-car, seq-cdr (and possibly seq-null?)
     21to its local database and thus supplying support for a new sequence type.
    2222
    2323The fundamental bind macro is given in two forms, whith and without a
     
    3333* a symbol,
    3434* a literal (char, string, keyword, boolean or number)
     35* a dotted list
    3536* a pair of patterns.
     37
     38where dotted lists (i.e patterns followed by two, tree or four dots)
     39are new in version 4.0.
    3640
    3741seq is a nested sequence expression, i.e. a mixture of pseudolists,
     
    4751same macro.
    4852
    49 Notice the meaning of dots following an expression in the sequel.
    50 
    51 * two dots: the expression appears zero or one times,
     53Dots might not only follow patterns but also expressions in documentation.
     54Their meaning is
     55
     56* two dots: the expression to the left appears zero or one times,
    5257* three dots: zero or many times,
    5358* four dots: one or many times.
     
    6772
    6873This is a generic procedure. It is closed over a local database which
    69 contains the necessary sequence versions of car and cdr.
     74contains the necessary sequence versions of car and cdr and possibly
     75null?
    7076
    7177<procedure>(bind-listify*)</procedure>
     
    7581<procedure>(bind-listify* seq)</procedure>
    7682
    77 returns the car-cdr-pair corresponding to seq's type.
     83returns the car-cdr-(respectivley car-cdr-null?)-list corresponding
     84to seq's type.
    7885
    7986<procedure>(bind-listify* pat seq)</procedure>
    8087
    8188transforms a nested pseudolist with possible wildcard, literals
    82 and dotted ends to a an ordinary nested list without.
     89and dotted lists to a an ordinary nested list without.
    8390
    8491<procedure>(bind-listify* seq? seq-car seq-cdr)</procedure>
     
    8794sequence variants of car and cdr to the internal database.
    8895
     96<procedure>(bind-listify* seq? seq-car seq-cdr seq-null?)</procedure>
     97
     98the same as before but with a null?-variant of seq.
     99
    89100==== vector-car
    90101
     
    99110vector-variant of cdr.
    100111
     112==== vector-null?
     113
     114<procedure>(vector-null? vec)</procedure>
     115
     116vector-variant of null?.
     117
    101118==== string-car
    102119
     
    111128string-variant of cdr.
    112129
     130==== string-null?
     131
     132<procedure>(string-null? vec)</procedure>
     133
     134string-variant of null?.
     135
    113136=== Binding macros
    114137
     
    118141
    119142with body:
    120 binds pattern variables of of a flat patern without wildcard, literals
     143binds pattern variables of of a nested list patern without wildcard, literals
    121144and dotted ends, pat,
    122 to corresponding values of a flat list
     145to corresponding values of a nested list
    123146and executes the body in this context.
    124147
    125148withoud body:
    126 set!s pattern variables of a flat patern without wildcard, literals
    127 and dotted ends, pat, to corresponding values of a flat list.
     149set!s pattern variables of a nested list patern without wildcard, literals
     150and dotted ends, pat, to corresponding values of a nested list.
    128151
    129152==== bind-list!
     
    134157the former is an alias to bind-list without body,
    135158the latter is (bind-list! pat 'pat)
    136 
    137 ==== bind-list*
    138 
    139 <macro>(bind-list* pat lst . body)</macro>
    140 
    141 nested versions of bind-list.
    142159
    143160==== bind
     
    270287;; reset local database to nested pseudolists only
    271288(bind-listify*)
     289
    272290;; add vector and string support
    273291(bind-listify* vector? vector-car vector-cdr)
     
    283301(bind-listify* '(a (_ b _) . c) '(1 #(20 30 40) 5))
    284302;-> '(1 (30) (5)))
    285 
    286 (bind-list* (x (y (z))) '(1 (2 (3))) (list x y z))
     303(bind-listify* '(x y as ... b c) '(-2 -1 1 2 3 40 50))
     304;-> '(-2 -1 (1 2 3) 40 50)
     305(bind-listify* '(x y as .. b c) '(-2 -1 40 50))
     306;-> '(-2 -1 () 40 50)
     307(bind-listify* '((as (bs cs)) ... d e) '((1 (2 3)) (10 (20 30)) 4 5))
     308;-> '(((1 10) ((2 20) (3 30))) 4 5)
     309
     310(bind-list (x (y (z))) '(1 (2 (3))) (list x y z))
    287311; -> '(1 2 3)
    288312
     
    320344; -> 1
    321345
     346(bind (x y as .... d e) '(-1 0 1 2 3 4 5) (list x y as d e))
     347;-> '(-1 0 (1 2 3) 4 5)
     348
     349(bind (x y as .. d e) '(-1 0 4 5)  (list x y as d e))
     350;-> '(-1 0 () 4 5)
     351
     352(bind ((as (bs cs)) ... d e)
     353      '((1 (2 3)) (10 (20 30)) 4 5)
     354      (list as bs cs d e))
     355;-> '((1 10) (2 20) (3 30) 4 5)
     356
    322357(bind (x y z w) '(1 2 3 4) (list x y z w))
    323358; -> '(1 2 3 4)
     
    339374; -> #t
    340375
     376((bind-lambda (a (b cs ...) ds ...)
     377   (list a b cs ds))
     378 '(1 #(20 30 40) 2 3))
     379;-> '(1 20 (30 40) (2 3))
     380
     381((bind-lambda (a (b (cs ds) ...) . es)
     382   (list a b cs ds es))
     383 '(1 #(20 (30 40)) 2 3))
     384;-> '(1 20 (30) (40) (2 3))
     385
    341386((bind-lambda (a (b . c) . d)
    342387   (list a b c d))
     
    350395
    351396(bind-loop (x (a . b) y) '(5 #(1) 0)
    352   (>> x integer?)
    353397  (if (zero? x)
    354398    (list x a b y)
     
    357401
    358402(bind* loop (x (a . b) y) '(5 #(1) 0)
    359   (>> x integer?)
    360403  (if (zero? x)
    361404    (list x a b y)
     
    387430; -> '(1 2 3 4 5 6)
    388431
     432(bind-let ((((x y) (zs ..)) '(#(1 2) ()))
     433           (((us vs) ...) '((3 4) (30 40) (300 400))))
     434  (list x y zs us vs))
     435;-> '(1 2 () (3 30 300) (4 40 400))
     436
    389437(bind-let loop (((a b) '(5 0)))
    390   (>> a integer?)
    391438  (if (zero? a)
    392439    (list a b)
    393440    (loop (list (- a 1) (+ b 1)))))
    394 ; -> '(0 5)
    395 
    396 (bind-let loop (
    397   ((x . y) '(1 2 3))
    398   ((z) '#(10))
    399   )
    400   (>> x integer?) (>> y (list-of? integer?)) (>> z integer?)
     441-> '(0 5)
     442
     443(bind-let loop (((x . y) '(1 2 3))
     444                ((z) #(10)))
    401445  (if (zero? z)
    402446    (list x y z)
    403447    (loop (cons (+ x 1) (map add1 y)) (list (- z 1)))))
    404 ; -> '(11 (12 13) 0)
     448-> '(11 (12 13) 0)
    405449
    406450(bind-let* (
     
    415459  (vector (list (lambda (m) (if (zero? m) #f (e? (- m 1)))))
    416460          (lambda (n) (if (zero? n) #t (o? (- n 1)))))
    417   (>> o? procedure?) (>> e? procedure?)
    418461  (list (o? 95) (e? 95)))
    419462; -> '(#t #f)
     
    429472; -> #t
    430473
     474(bindable? (a (b cs .. d)) '(1 (2 3)))
     475;-> #t
     476
    431477((bindable? (a (b c) . d)) '(1 (2 3) . 4))
    432478; -> #t
     
    448494
    449495(bind-case #(1 2)
    450   (() '())
    451   ((a) (list a))
     496  (() #f)
     497  ((a) #f)
    452498  ((a b) (list a b))
    453   ((a b c) (list a b c)))
     499  ((a b c) #f))
    454500; -> '(1 2))
    455501
    456502(bind-case #(2 2)
    457   ((a b) (where (even? a) (odd? b)) (print 'even-odd a b))
    458   ((a b) (where (odd? a) (even? b)) (print 'odd-even a b))
     503  ((a b) (where (even? a) (odd? b)) #f)
     504  ((a b) (where (odd? a) (even? b)) #f)
    459505  ((a b) (list a b)))
    460506; -> '(2 2)
     507
     508(bind-case '(0 4)
     509  ((a bs .... c) #f)
     510  ((a bs ... c) (list a bs c)))
     511;-> '(0 () 4)
     512
     513(bind-case '(0 1 2 3 4)
     514  ((a bs .. c) #f)
     515  ((a bs ... c) (list a bs c)))
     516;-> '(0 (1 2 3) 4)
     517
     518(bind-case '(0 #(1 (2 3)) 4)
     519  ((a (bs (cs (ds))) .. e) #f)
     520  ((a (bs (cs ds)) .. e) (list a bs cs ds e)))
     521;-> '(0 (1) (2) (3) 4)
    461522
    462523(define (my-map fn lst)
     
    482543((bind-case-lambda
    483544   ((a (b . c) . d) (list a b c d))
    484    ((e . f) (>> e zero?) e)
     545   ((e . f) (where (zero? e)) e)
    485546   ((e . f) (list e f)))
    486547 '(1 2 3 4 5))
     
    488549
    489550((bind-case-lambda
    490    ((e . f) (>> e zero?) f)
     551   ((e . f) (where (zero? e)) f)
    491552   ((e . f) (list e f)))
    492553 #(0 2 3 4 5))
     
    515576== Last update
    516577
    517 Apr 25, 2020
     578Jun 27, 2020
    518579
    519580== Author
     
    553614
    554615== Version History
     616; 4.0 ; dotted patterns added
     617; 3.x ; bind-list* removed, bind-list does its job for nested lists only ;;;;;;
    555618; 3.2 : bindable? now in two forms, bind-case improved, both with optional fenders.
    556619; 3.1 : bind* now deprecated, use bind-loop instead.
Note: See TracChangeset for help on using the changeset viewer.