Changeset 37753 in project


Ignore:
Timestamp:
07/04/19 13:11:11 (4 months ago)
Author:
juergen
Message:

list-comprehensions 1.1 with curried and uncurried versions, syntax change of for

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/list-comprehensions

    r37517 r37753  
    1212
    1313Other list-generating procedures, like range, repeat, iterate-times,
    14 iterate-until and iterate-while are provided as well.
     14iterate-until and iterate-while are provided as well, in a curried and
     15an uncurried version. In the sequel, only the uncurried version is
     16documented, the curried version is supplied to be used with map and
     17friends.
    1518
    1619=== The API
     
    3639
    3740<procedure>(repeat times)</procedure>
     41<procedure>(repeat times x)</procedure>
    3842
    3943returns a unary procedure which repeats its only argument
     
    4347
    4448<procedure>(iterate-times fn times)</procedure>
     49<procedure>(iterate-times fn times start)</procedure>
    4550
    4651returns a unary procedure which iterates the function fn
     
    5055
    5156<procedure>(iterate-while fn ok?)</procedure>
     57<procedure>(iterate-while fn ok? start)</procedure>
    5258
    5359returns a unary procedure which iterates the function fn
     
    5763
    5864<procedure>(iterate-until fn ok?)</procedure>
     65<procedure>(iterate-until fn ok? start)</procedure>
    5966
    6067returns a unary procedure which iterates the function fn
     
    6370==== for
    6471
    65 <macro>(for (qualifier ....) item)</macro>
     72</macro>(for item qualifier ....)</macro>
    6673
    6774where each qualifier is of the form (var lst fltr ...) whith
     
    8188
    8289<enscript highlight=scheme>
    83   (for ((x (list #\A #\B))
    84         (y (list 1 2))
    85         (z (list #f #t)))
    86        (list x y z))
     90  (for (list x y z)
     91       (x (list #\A #\B))
     92       (y (list 1 2))
     93       (z (list #f #t)))
    8794</enscript>
    8895
     
    122129(range 1 5 2) ; -> '(1 3)
    123130
    124 ((repeat 5) 'x) ; -> '(x x x x x)
     131(repeat 5 'x) ; -> '(x x x x x)
    125132
    126 ((iterate-times add1 5) 1) ; -> '(1 2 3 4 5)
     133(iterate-times add1 5 1) ; -> '(1 2 3 4 5)
    127134
    128 ((iterate-until sub1 zero?) 5) ; -> '(5 4 3 2 1)
     135(iterate-until sub1 zero? 5) ; -> '(5 4 3 2 1)
    129136
    130 ((iterate-while sub1 positive?) 5) ; -> '(5 4 3 2 1)
     137(iterate-while sub1 positive? 5) ; -> '(5 4 3 2 1)
    131138
    132 (for ((c (string->list "ABCDEFG"))
    133       (k '(1 2 3 4 5 6 7 8)))
    134      (list c k))
     139(for (list c k)
     140     (c (string->list "ABCDEFG"))
     141     (k '(1 2 3 4 5 6 7 8)))
    135142; -> ((#\A 1) ... (#\A 8) ... (#\G 1) ... (#\G 8))
    136143
    137 (for ((c (string->list "ABCDEFG"))
    138       (k '(1 2 3 4 5 6 7 8) (memv c '(#\A #\G)) (memv k '(1 8))))
    139      (list c k))
     144(for (list c k)
     145     (c (string->list "ABCDEFG"))
     146     (k '(1 2 3 4 5 6 7 8) (memv c '(#\A #\G)) (memv k '(1 8))))
    140147; -> ((#\A 1) (#\A 8) (#\G 1) (#\G 8))
    141148
    142 (for ((x '(0 1 2 3))) (add1 x)) ; map
     149(for (add1 x) (x '(0 1 2 3))) ; map
    143150; -> '(1 2 3 4)
    144151
    145 (for ((x '(0 1 2 3 4 5) (odd? x))) x) ; filter
     152(for x (x '(0 1 2 3 4 5) (odd? x))) ; filter
    146153; -> '(1 3 5)
    147154</enscript>
     
    149156== Last update
    150157
    151 Jan 28, 2019
     158Jul 04, 2019
    152159
    153160== Author
     
    187194
    188195== Version History
     196; 1.1 : curried and uncurried versions, syntax of for changed
    189197; 1.0 : initial import for chicken-5
    190198
Note: See TracChangeset for help on using the changeset viewer.