source: project/wiki/eggref/5/simple-loops @ 37400

Last change on this file since 37400 was 37400, checked in by juergen, 3 years ago

simple-loops 1.0

File size: 3.5 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4== simple-loops
5
6This module exports some simple loop macros.
7
8=== The routines
9
10==== simple-loops
11
12<procedure>(simple-loops [sym])</procedure>
13
14documentation-procedure; returns the list of exported symbols if called
15with no symbol, otherwise the signature of that exported symbol sym.
16
17==== do-for
18
19<macro>(do-for var (start stop step) xpr . xprs)</macro>
20for-loop:
21do xpr . xprs for var in [start stop[ with steps (default 1)")
22
23==== do-forever
24
25<macro>(do-forever xpr . xprs)</macro>
26endless loop:
27executes body xpr . xprs until exit is called
28
29==== do-list
30
31<macro>(do-list i lst xpr . xprs)</macro>
32loop along a list:
33execute xpr . xprs for i in lst
34
35==== do-times
36
37<macro>(do-times i upto xpr . xprs)</macro>
38loops a fixed number of times:
39execute xpr . xprs for i in [0 upto[
40
41==== do-until
42
43<macro>(do-until test? xpr . xprs)</macro>
44until-loop:
45execute xpr . xprs while test? is false
46
47==== do-while
48
49<macro>(do-while test? xpr . xprs)</macro>
50while-loop:
51execute xpr . xprs while test? is true
52
53=== Requirements
54
55none
56
57=== Example
58
59<enscript highlight=scheme>
60
61(import simple-loops)
62
63    (let ((lst '()))
64      (do-for i (1 65 i) (set! lst (cons i lst)))
65      (reverse lst))
66    ;-> '(1 2 4 8 16 32 64)
67
68    (let ((n 3) (lst '()))
69      (do-forever
70        (if (zero? n) (exit lst))
71        (set! lst (cons 'a lst))
72        (set! n (- n 1))))
73    ;-> '(a a a)
74
75    (let ((lst '()))
76      (do-list i '(1 2 3)
77        (set! lst (cons i lst)))
78      lst)
79    ;-> '(3 2 1)
80
81    (let ((lst '()))
82      (do-times i (+ 2 3)
83        (set! lst (cons i lst)))
84      lst)
85    ;-> '(4 3 2 1 0)
86
87    (let ((n 4) (lst '()))
88      (do-until (> 0 n)
89        (set! lst (cons n lst))
90        (set! n (- n 1)))
91      lst)
92    ;-> '(0 1 2 3 4)
93
94    (let ((n 4) (lst '()))
95      (do-while (<= 0 n)
96        (set! lst (cons n lst))
97        (set! n (- n 1)))
98      lst)
99    ;-> '(0 1 2 3 4)
100
101</enscript>
102
103== Last update
104
105Mar 17, 2019
106
107== Author
108
109[[/users/juergen-lorenz|Juergen Lorenz]]
110
111== License
112
113 Copyright (c) 2013-2019, Juergen Lorenz
114 All rights reserved.
115
116 Redistribution and use in source and binary forms, with or without
117 modification, are permitted provided that the following conditions are
118 met:
119 
120 Redistributions of source code must retain the above copyright
121 notice, this list of conditions and the following disclaimer.
122 
123 Redistributions in binary form must reproduce the above copyright
124 notice, this list of conditions and the following disclaimer in the
125 documentation and/or other materials provided with the distribution.
126 Neither the name of the author nor the names of its contributors may be
127 used to endorse or promote products derived from this software without
128 specific prior written permission.
129   
130 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
131 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
132 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
133 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
134 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
135 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
136 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
137 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
138 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
139 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
140 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141
142== Version History
143
144; 1.0 : port from chicken-4
Note: See TracBrowser for help on using the repository browser.