[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#14482: Macro not able to see a function
From: |
Ludovic Courtès |
Subject: |
bug#14482: Macro not able to see a function |
Date: |
Thu, 30 May 2013 23:21:59 +0200 |
User-agent: |
Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux) |
tags 14482 notabug
thanks
Hi,
Sanel Zukan <address@hidden> skribis:
> ---------------------------------------
>
> (define (partition-two lst)
> (if (< (length lst) 2)
> '()
> (cons
> (list (car lst) (cadr lst))
> (partition-two (cddr lst)))))
>
> (define-macro (letn bindings . body)
> `(let* ,(partition-two bindings)
> ,@body))
>
> (define (println v)
> (display v)
> (newline))
>
> (println
> (letn (a 3
> b 4
> c 4
> d a
> f (+ a b))
> (+ a b c d f)))
>
> ---------------------------------------
>
> It correctly prints result, but I'm getting also:
>
> ---------------------------------------
> ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
> ;;; or pass the --no-auto-compile argument to disable.
> ;;; compiling /home/sanel/letn.ss
> ;;; WARNING: compilation of /home/sanel/letn.ss failed:
> ;;; ERROR: Unbound variable: partition-two
> 21
> ---------------------------------------
That’s because ‘partition-two’ is only visible at run time, and not at
macro-expansion time.
To fix that, use the ‘eval-when’ form:
--8<---------------cut here---------------start------------->8---
(eval-when (load compile eval)
(define (partition-two lst)
(if (< (length lst) 2)
'()
(cons
(list (car lst) (cadr lst))
(partition-two (cddr lst))))))
--8<---------------cut here---------------end--------------->8---
(BTW, I recommend looking at R5RS ‘syntax-rules’ and R6RS ‘syntax-case’
hygienic macros.)
Ludo’.