The followine code is accepted without error when pasted into the REPL, but generates this error when included from a file:
*** ERROR -- Unbound variable: nthcdr
(define (firstn n lst) (cond ((not n) lst) ((and (positive? n) (pair? lst)) (cons (car lst) (firstn (- n 1) (cdr lst)))) (else '())))
(define (nthcdr n lst) (cond ((not n) lst) ((or (< n 1) (null? lst)) lst) (else (nthcdr (- n 1) (cdr lst)))))
(define-macro (%with% sequential? var-val-list . body) (let ((paired (let recur ((lst var-val-list) (acc '())) (if (null? lst) (reverse acc) ;; In line below, nthcdr is unbound! --------------------- (recur (nthcdr 2 lst) (cons (firstn 2 (append lst '('()))) acc)))))) (if sequential? `(let* ,paired ,@body) `(let ,paired ,@body)))) (define-macro (with var-val-list . body) `(%with% #f ,var-val-list ,@body)) (define-macro (with* var-val-list . body) `(%with% #t ,var-val-list ,@body))
(define (foo) (with (a 2 b 9) (println b)))
This is with Gambit v4.6.1 under Windows.
Afficher les réponses par date
This would be help: https://mercure.iro.umontreal.ca/pipermail/gambit-list/2006-May/000690.html
nthcdr will be set at runtime, while macro will be evaluate before it.
--- On Sun, 5/1/11, William James w_a_x_man@yahoo.com wrote:
From: William James w_a_x_man@yahoo.com Subject: [gambit-list] Pasting into REPL good; including file bad To: gambit-list@iro.umontreal.ca Date: Sunday, May 1, 2011, 4:02 AM The followine code is accepted without error when pasted into the REPL, but generates this error when included from a file:
The solution suggested was (define-macro (at-expand-time expr) (eval expr) `(begin))
That works---up to a point.
When a function in file X is wrapped in that macro, when compiling the function is not visible in file Y if file Y includes file X.
On usenet, it was stated that Chez Scheme handles the problem this way:
(meta define (nthcdr n lst) ---)
What is the solution in Gambit?