Change that (begin ...) to a (let () ...) and guile will do what you want.
-- Linus Björnstam On Sat, 12 Jul 2025, at 11:12, 胡峻豪 wrote: > Sorry I missed your previous emails these days. > > > > I've recently become aware of several solutions discussed, which have > been very helpful. > > > > I see significant progress reflected in Guile's change notes, and the > guile-define work also contains valuable insights. What I'm hoping to > achieve is the ability to run code like this: > > > > scheme > > (define x 1) > > (do ((i 0 (+ i 1))) > > ((= i 2)) > > (begin > > (define x 2) > > (display x) > > ) > > ) > > (display x) > > Expected Output: 221 > > > > Currently, Guile throws an error for this, whereas Elk Scheme executes > it successfully. (Elk reference: https://sam.zoy.org/elk/) > > > > Thank you again for your responses. > > > > > At 2025-07-09 22:36:24, "Linus Björnstam" <linus.inter...@fastmail.se> wrote: >>Just fyi: The latest guile 3.0.10 has definitions in almost all definition >>contexts. If i recall correctly I didnt add it to one of the old looping >>constructs. >>-- >> Linus Björnstam >> >>On Sat, 5 Jul 2025, at 20:15, 胡峻豪 wrote: >>> I'm a newcomer to Guile and am currently using Guile to bind some C++ >>> functions for users. Users already have many Scheme scripts, but their >>> scripts don't comply with standards—for example, using define in >>> expression contexts, which causes Guile to throw errors. I tried >>> writing a code snippet with Claude to improve this, as shown below >>> >>> >>> (use-modules (ice-9 regex)) >>> >>> >>> (define-syntax original-define >>> (identifier-syntax define)) >>> >>> >>> ;; Redefine define to avoid recursive calls >>> (define-syntax define >>> (lambda (stx) >>> (syntax-case stx () >>> ((_ var val) >>> #'(begin >>> (module-define! (current-module) 'var val) >>> var)) >>> ((_ (name . args) . body) >>> #'(begin >>> (module-define! (current-module) 'name (lambda args . body)) >>> name))))) >>> >>> >>> But this makes the definitions module-level. If I define the same >>> variable at the top level and inside a lambda, they overwrite each >>> other. Scheme is too difficult for me. Is there any way to achieve this >>> functionality without modifying the scripts? I would appreciate your >>> help.