From: Deepak Surti
Subject: Facing problem with if-match macro from Paul Graham's On Lisp
Date: 
Message-ID: <4bd2a359-46e6-4214-919c-be2e35a3d4e0@q9g2000yqc.googlegroups.com>
I am working through desctructuring chapter 18 of PG's On Lisp book.
He defines a pattern matching version of dbind like this:

(defmacro if-match (pat seq then &optional else)
  `(aif2 (match ',pat ,seq)
	 (let ,(mapcar #'(lambda (v)
			   `(,v (binding ',v it)))
		       (vars-in then #'atom))
	   ,then)
	 ,else))

I have typed out all related functions like vars-in, var? etc (not
shown here).

Then he shows an example function abab where he uses the above macro.

I typed out the abab function as well as shown below:

(defun abab (seq)
  (if-match (?x ?y ?x ?y) seq
	    (values ?x ?y)
	    nil))

However after say I have shut down my Emacs and later restart it and
compile this file
which has all code, it gives me the error on abab as shown:

Compilation failed: 1 error  0 warnings 14 style-warnings  0 notes
[1.17 secs]
(in macroexpansion of (IF-MATCH (?X ?Y ?X ...) SEQ ...))
(hint: For more precise location, try *BREAK-ON-SIGNALS*.)
The function VARS-IN is undefined.

Now if I compile C-c C-c vars-in and var? etc then compile abab it
gets compiled. Why does the if-match expansion fail when I do a
compile file C-c, C-k.

Any ideas to help me resolve this would be great? Pls forgive if it is
a stupid error on my part.

Thanks a ton in advance,
Regards,
Deepak Surti

From: Deepak Surti
Subject: Re: Facing problem with if-match macro from Paul Graham's On Lisp
Date: 
Message-ID: <3295ecb1-c36b-41ae-9f7e-4e45e0757717@h28g2000yqd.googlegroups.com>
I forgot to mention. I am using SBCL.

On Apr 21, 4:56 pm, Deepak Surti <·······@gmail.com> wrote:
> I am working through desctructuring chapter 18 of PG's On Lisp book.
> He defines a pattern matching version of dbind like this:
>
> (defmacroif-match(pat seq then &optional else)
>   `(aif2 (match ',pat ,seq)
>          (let ,(mapcar #'(lambda (v)
>                            `(,v (binding ',v it)))
>                        (vars-in then #'atom))
>            ,then)
>          ,else))
>
> I have typed out all related functions like vars-in, var? etc (not
> shown here).
>
> Then he shows an example function abab where he uses the above macro.
>
> I typed out the abab function as well as shown below:
>
> (defun abab (seq)
>   (if-match(?x ?y ?x ?y) seq
>             (values ?x ?y)
>             nil))
>
> However after say I have shut down my Emacs and later restart it and
> compile this file
> which has all code, it gives me the error on abab as shown:
>
> Compilation failed: 1 error  0 warnings 14 style-warnings  0 notes
> [1.17 secs]
> (in macroexpansion of (IF-MATCH(?X ?Y ?X ...) SEQ ...))
> (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
> The function VARS-IN is undefined.
>
> Now if I compile C-c C-c vars-in and var? etc then compile abab it
> gets compiled. Why does theif-matchexpansion fail when I do a
> compile file C-c, C-k.
>
> Any ideas to help me resolve this would be great? Pls forgive if it is
> a stupid error on my part.
>
> Thanks a ton in advance,
> Regards,
> Deepak Surti
From: Tomas Zellerin
Subject: Re: Facing problem with if-match macro from Paul Graham's On Lisp
Date: 
Message-ID: <7db4afc7-8039-4f6f-88c1-b34033431913@c9g2000yqm.googlegroups.com>
On 21 Dub, 13:59, Deepak Surti <·······@gmail.com> wrote:
> ...
> > However after say I have shut down my Emacs and later restart it and
> > compile this file
> > which has all code, it gives me the error on abab as shown:
>
> > Compilation failed: 1 error  0 warnings 14 style-warnings  0 notes
> > [1.17 secs]
> > (in macroexpansion of (IF-MATCH(?X ?Y ?X ...) SEQ ...))
> > (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
> > The function VARS-IN is undefined.
>

Macro is expanded in compile time, and the functions are not known at
that time. Either use eval-when, or separate the definitions to
different file.

Try look up eval-when in Practical Common Lisp, for example.

Regards,
Tomas
From: Deepak Surti
Subject: Re: Facing problem with if-match macro from Paul Graham's On Lisp
Date: 
Message-ID: <2536d79f-0f03-4f27-937b-3f7ac6f7fda7@x5g2000yqk.googlegroups.com>
On Apr 21, 5:15 pm, Tomas Zellerin <········@gmail.com> wrote:
> On 21 Dub, 13:59, Deepak Surti <·······@gmail.com> wrote:
>
> > ...
> > > However after say I have shut down my Emacs and later restart it and
> > > compile this file
> > > which has all code, it gives me the error on abab as shown:
>
> > > Compilation failed: 1 error  0 warnings 14 style-warnings  0 notes
> > > [1.17 secs]
> > > (in macroexpansion of (IF-MATCH(?X ?Y ?X ...) SEQ ...))
> > > (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
> > > The function VARS-IN is undefined.
>
> Macro is expanded in compile time, and the functions are not known at
> that time. Either use eval-when, or separate the definitions to
> different file.
>
> Try look up eval-when in Practical Common Lisp, for example.
>
> Regards,
> Tomas

Hi Tomas,

Separating out functions into another file did the trick.

Thanks for the quick response. I will surely go through PCL as pointed
out by you.

Regards,
Deepak