an old story those "define in expression context" i remember, having
redefined a lot of thing to made use of define inside:
(define-module (Scheme+ while-do)
#:use-module ((guile) #:select ((do . do-scheme)
(while . while-guile)))
#:replace (do while))
;; > (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)
((do ((variable init step ...) ...) (test expr ...) body ...)
(do-scheme ((variable init step ...) ...) (test expr ...) body ...))
((do b1 ...
while pred)
(let loop () b1 ... (when pred (loop))))))
(define-syntax while
(syntax-rules ()
((while test body ...) (while-guile test
(let ()
body
...)))))
instead of begin you can use an empty (let () .... with definition inside,
this is allowed.....
On Sun, Jul 13, 2025 at 2:30 PM Maxime Devos <[email protected]> wrote:
>
> On 12/07/2025 11:12, 胡峻豪 wrote:
> > (do ((i 0 (+ i 1)))
> >
> > ((= i 2))
> >
> > (begin
> >
> > (define x 2)
> >
> > (display x)
> >
> > )
> >
> > )
>
> Can be done by redefining 'do'
>
> Basically, let '(new-do x y body ...)' be '(do x y (let () body ...))'
> and redefine 'do' by 'new-do' in some way (it is convenient to import
> the old 'do' under a different name). Or take the old definition of
> 'do', copy it, and insert a surrounding 'let' in the right lace.
>
> Same applies to many other situations.
>
> (Also, that 'begin' serves no purpose there.)
>
> Best regards,
> Maxime Devos
>
>
>