-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile-do.rkt
218 lines (154 loc) · 4.3 KB
/
while-do.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
;; This file is part of Scheme+
;; Copyright 2024 Damien MATTEI
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(module while-do racket/base
(provide while
do)
(require (only-in racket/base [do do-scheme])) ;; backup original Scheme 'do'
;; warning: 'do is already part of R6RS (reserved keyword) 'while is not in R5RS,R6RS, R7RS-small
;; but 'do in Scheme has a painful syntax
;; syntax defined in this file are inspired from Pascal language, C , Java,Javascript
;; scheme@(guile-user)> (use-modules (Scheme+R6RS))
;; scheme@(guile-user)> (define i 0)
;; scheme@(guile-user)> (define do '())
;; scheme@(guile-user)> (while {i < 4}
;; do
;; (display i)
;; (newline)
;; {i <- {i + 1}})
;; 0
;; 1
;; 2
;; 3
;; $1 = #f
;; (while {i < 4}
;; do
;; (display i)
;; (newline)
;; {i <- {i + 1}})
;; (define-syntax while
;; (syntax-rules (while do)
;; ((_ pred do b1 ...)
;; (let loop () (when pred b1 ... (loop))))))
;; bug: 1+ return #f with SRFI 105 reader of Racket
;; (do ((i 1 (1+ i))
;; (p 3 (* 3 p)))
;; ((> i 4)
;; p)
;; (format #t "3**~s is ~s\n" i p))
;; 3**1 is 3
;; 3**2 is 9
;; 3**3 is 27
;; 3**4 is 81
;; $1 = 243
;; scheme@(guile-user)> (do ((i 1 (1+ i))
;; (p 3 (* 3 p)))
;; ((> i 4)
;; p)
;; (set! p (+ p i)))
;; $1 = 417
;; with a definition inside only the new version works:
;; (do ((i 1 (+ 1 i))
;; (p 3 (* 3 p)))
;; ((> i 4)
;; p)
;; (define x 7)
;; (set! p (+ p i x)))
;; $3 = 1257
;; 'do is redefined here only to allow 'define in body as allowed in Scheme+R6RS
;; (define-syntax do
;; (syntax-rules ()
;; ((do ((var init step ...) ...)
;; (test expr ...)
;; command ...)
;; (letrec
;; ((loop
;; (lambda (var ...)
;; (if test
;; ;;(begin
;; (let ()
;; ;;#f ; avoid empty begin but with (let () i don't care ! (bigloo use: (if #f #f) which creates a never executed instruction !)
;; '() ;; avoid while-do-when-unless.scm: let: bad syntax (missing binding pairs or body) in: (let ())
;; expr ...)
;; ;;(begin
;; (let ()
;; command
;; ...
;; (loop (do "step" var step ...)
;; ...))))))
;; (loop init ...)))
;; ((do "step" x)
;; x)
;; ((do "step" x y)
;; y)))
;; > (define i 0)
;; > (do (display "toto") (newline) (set! i (+ i 1)) while (< i 4))
;; toto
;; toto
;; toto
;; toto
;; this 'do' do not break the one of scheme:
;; (do ((i 1 (+ i 1))
;; (p 3 (* 3 p)))
;; ((> i 4)
;; p)
;; (display p)(newline))
;; 3
;; 9
;; 27
;; 81
;; 243
;; > (do (define j i) (display "toto") (newline) (set! i (+ i 1)) while (< j 4))
;; toto
;; toto
;; toto
;; toto
;; toto
(define-syntax do
(syntax-rules (while)
;; original do from scheme
((do ((variable init step ...) ...) (test expr ...) body ...)
(do-scheme ((variable init step ...) ...) (test expr ...) body ...))
;; do from scheme+
((do b1 ...
while pred)
(let loop () b1 ... (when pred (loop))))))
;; > (define x 0)
;; > (while (< x 10)
;; (define y x)
;; (display y)
;; (newline)
;; (set! x (+ x 1)))
;; 0
;; 1
;; 2
;; 3
;; 4
;; 5
;; 6
;; 7
;; 8
;; 9
;; >
;; (define-syntax-rule (while condition body ...)
;; (let loop ()
;; (when condition
;; body ...
;; (loop))))
(define-syntax while
(syntax-rules ()
((while condition body ...)
(let loop ()
(when condition
body ...
(loop))))))
) ; end library