define use in expression context

胡峻豪 <[email protected]> Sun, 6 Jul 2025 02:15:04 +0800 (CST)
Newsgroups gmane.lisp.guile.user
Message-ID <[email protected]>
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.