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.