I am working through Ch. 3 of PCL, and I am stuck on the where
function (not the macro).
I am actually writing/transcribing the code for a similar application,
and my where function is:
(defun where (&key english japanese)
"return test for entry -- based on PCL, p.30"
#'(lambda (entry)
(and
(if english (equal (getf entry :english) english) t)
(if japanese (equal (getf entry :japanese) japanese) t)
When I execute, for example
(where :english "cold")
I get
#<FUNCTION :LAMBDA (ENTRY)
(AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
(IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>
But I would expect occurences of ENGLISH to be replaced with "COLD"
and of JAPANESE with NIL:
#<FUNCTION :LAMBDA (ENTRY)
(AND (IF "COLD" (EQUAL (GETF ENTRY :ENGLISH) "COLD") T) ;; expect
COLD, not ENGLISH
(IF NIL (EQUAL (GETF ENTRY :JAPANESE) NIL) T))>
So, what am I missing? In the current form, the function does not
work (when used as argument to select (another function in that
chapter)).
Thanks,
Mirko
·············@gmail.com wrote:
> When I execute, for example (where :english "cold")
> ... I would expect occurences of ENGLISH to be replaced with "COLD"
> and of JAPANESE with NIL
That's what macros do.
> So, what am I missing?
Closures are still variables. The value doesn't get hard-coded in.
--
Dan
www.prairienet.org/~dsb/
·············@gmail.com writes:
> I am working through Ch. 3 of PCL, and I am stuck on the where
> function (not the macro).
>
> I am actually writing/transcribing the code for a similar application,
> and my where function is:
>
> (defun where (&key english japanese)
> "return test for entry -- based on PCL, p.30"
> #'(lambda (entry)
> (and
> (if english (equal (getf entry :english) english) t)
> (if japanese (equal (getf entry :japanese) japanese) t)
>
>
> When I execute, for example
> (where :english "cold")
> I get
> #<FUNCTION :LAMBDA (ENTRY)
> (AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
> (IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>
>
> But I would expect occurences of ENGLISH to be replaced with "COLD"
> and of JAPANESE with NIL:
> #<FUNCTION :LAMBDA (ENTRY)
> (AND (IF "COLD" (EQUAL (GETF ENTRY :ENGLISH) "COLD") T) ;; expect
> COLD, not ENGLISH
> (IF NIL (EQUAL (GETF ENTRY :JAPANESE) NIL) T))>
>
> So, what am I missing?
As Dan said, closures keep the variables and don't substitute them,
they just bind them in a closure environment. To get a substitution,
you'd need a macro.
What's confusing is that closures are not printed explicitely, dumping
their whole environment.
(where :english "cold") could print as:
#<CLOSURE ((ENGLISH . "cold") (JAPANESE . NIL))
#<FUNCTION :LAMBDA (ENTRY)
(AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
(IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>>
> In the current form, the function does not work (when used as
> argument to select (another function in that chapter)).
To me it seems to work perfectly:
C/USER[45]> (funcall (where :english "cold") '(:french "froid" :english "warm"))
NIL
C/USER[46]> (funcall (where :english "cold") '(:french "froid" :english "cold"))
T
--
__Pascal Bourguignon__ http://www.informatimago.com/
IMPORTANT NOTICE TO PURCHASERS: The entire physical universe,
including this product, may one day collapse back into an
infinitesimally small space. Should another universe subsequently
re-emerge, the existence of this product in that universe cannot be
guaranteed.
On Jul 26, 5:30 am, Pascal Bourguignon <····@informatimago.com> wrote:
> ·············@gmail.com writes:
> > I am working through Ch. 3 of PCL, and I am stuck on the where
> > function (not the macro).
>
> > I am actually writing/transcribing the code for a similar application,
> > and my where function is:
>
> > (defun where (&key english japanese)
> > "return test for entry -- based on PCL, p.30"
> > #'(lambda (entry)
> > (and
> > (if english (equal (getf entry :english) english) t)
> > (if japanese (equal (getf entry :japanese) japanese) t)
>
> > When I execute, for example
> > (where :english "cold")
> > I get
> > #<FUNCTION :LAMBDA (ENTRY)
> > (AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
> > (IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>
>
> > But I would expect occurences of ENGLISH to be replaced with "COLD"
> > and of JAPANESE with NIL:
> > #<FUNCTION :LAMBDA (ENTRY)
> > (AND (IF "COLD" (EQUAL (GETF ENTRY :ENGLISH) "COLD") T) ;; expect
> > COLD, not ENGLISH
> > (IF NIL (EQUAL (GETF ENTRY :JAPANESE) NIL) T))>
>
> > So, what am I missing?
>
> As Dan said, closures keep the variables and don't substitute them,
> they just bind them in a closure environment. To get a substitution,
> you'd need a macro.
>
> What's confusing is that closures are not printed explicitely, dumping
> their whole environment.
>
> (where :english "cold") could print as:
>
> #<CLOSURE ((ENGLISH . "cold") (JAPANESE . NIL))
> #<FUNCTION :LAMBDA (ENTRY)
> (AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
> (IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>>
>
> > In the current form, the function does not work (when used as
> > argument to select (another function in that chapter)).
>
> To me it seems to work perfectly:
>
> C/USER[45]> (funcall (where :english "cold") '(:french "froid" :english "warm"))
> NIL
> C/USER[46]> (funcall (where :english "cold") '(:french "froid" :english "cold"))
> T
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> IMPORTANT NOTICE TO PURCHASERS: The entire physical universe,
> including this product, may one day collapse back into an
> infinitesimally small space. Should another universe subsequently
> re-emerge, the existence of this product in that universe cannot be
> guaranteed.
Yes, I think I am mixing the effects of macros and functions. I'll
play a bit more with this function and Pascal's example of usage.
Thank you both.
Mirko
On Jul 26, 7:42 am, ·············@gmail.com wrote:
> On Jul 26, 5:30 am, Pascal Bourguignon <····@informatimago.com> wrote:
>
>
>
> > ·············@gmail.com writes:
> > > I am working through Ch. 3 of PCL, and I am stuck on the where
> > > function (not the macro).
>
> > > I am actually writing/transcribing the code for a similar application,
> > > and my where function is:
>
> > > (defun where (&key english japanese)
> > > "return test for entry -- based on PCL, p.30"
> > > #'(lambda (entry)
> > > (and
> > > (if english (equal (getf entry :english) english) t)
> > > (if japanese (equal (getf entry :japanese) japanese) t)
>
> > > When I execute, for example
> > > (where :english "cold")
> > > I get
> > > #<FUNCTION :LAMBDA (ENTRY)
> > > (AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
> > > (IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>
>
> > > But I would expect occurences of ENGLISH to be replaced with "COLD"
> > > and of JAPANESE with NIL:
> > > #<FUNCTION :LAMBDA (ENTRY)
> > > (AND (IF "COLD" (EQUAL (GETF ENTRY :ENGLISH) "COLD") T) ;; expect
> > > COLD, not ENGLISH
> > > (IF NIL (EQUAL (GETF ENTRY :JAPANESE) NIL) T))>
>
> > > So, what am I missing?
>
> > As Dan said, closures keep the variables and don't substitute them,
> > they just bind them in a closure environment. To get a substitution,
> > you'd need a macro.
>
> > What's confusing is that closures are not printed explicitely, dumping
> > their whole environment.
>
> > (where :english "cold") could print as:
>
> > #<CLOSURE ((ENGLISH . "cold") (JAPANESE . NIL))
> > #<FUNCTION :LAMBDA (ENTRY)
> > (AND (IF ENGLISH (EQUAL (GETF ENTRY :ENGLISH) ENGLISH) T)
> > (IF JAPANESE (EQUAL (GETF ENTRY :JAPANESE) JAPANESE) T))>>
>
> > > In the current form, the function does not work (when used as
> > > argument to select (another function in that chapter)).
>
> > To me it seems to work perfectly:
>
> > C/USER[45]> (funcall (where :english "cold") '(:french "froid" :english "warm"))
> > NIL
> > C/USER[46]> (funcall (where :english "cold") '(:french "froid" :english "cold"))
> > T
>
> > --
> > __Pascal Bourguignon__ http://www.informatimago.com/
>
> > IMPORTANT NOTICE TO PURCHASERS: The entire physical universe,
> > including this product, may one day collapse back into an
> > infinitesimally small space. Should another universe subsequently
> > re-emerge, the existence of this product in that universe cannot be
> > guaranteed.
>
> Yes, I think I am mixing the effects of macros and functions. I'll
> play a bit more with this function and Pascal's example of usage.
>
> Thank you both.
>
> Mirko
I just read the definition of closure on hyper-lisp. My head is still
spinning :-) It seems like time-warp.
But, I don't have a better way of putting what it is trying to say.
Thanks again,
Mirko