From: Michael Tebeka
Subject: Lambda expressions
Date: 
Message-ID: <Pine.A32.3.96.981116135941.49196A-100000@ilx020.iil.intel.com>
Hello All,

Can anyone tell me where do I go wrong here?

When I try in Scheme:
 (define (make-point x y)
    (lambda (msg)
      (cond ((equal? msg 'x) x)
            ((equal? msg 'y) y))))

 (define p (make-point 1 2))

It works:

 (p 'x) => 1
 (p 'y) => 2

But when I try in Lisp:
(defun make-point (x y)
     '(lambda (msg)
        (cond ((equal msg 'x) x)
              ((equal msg 'y) y))))

(setq p (make-point 1 2)

It doesn't work:

 (apply p '(x)) => ;; Error: Unbound variable X in #<function 1 #x10BDE44>

I guess that the variable scope is a bit different, if so how do I make it
work in Lisp?

Thanks.

     O O      Smile, damn it, smile.
    \___/    
                  Miki Tebeka
               ······@geocities.com	
     http://www.geocities.com/Yosemite/Trails/8351

      In a Bangkok dry cleaner's:
             Drop your trousers here for best results.
      

From: Donald Fisk
Subject: Re: Lambda expressions
Date: 
Message-ID: <36507B43.163BB281@bt-sys.spamblock.bt.co.uk>
Michael Tebeka wrote:

> Hello All,
>
> Can anyone tell me where do I go wrong here?
>
> I guess that the variable scope is a bit different, if so how do I make it
> work in Lisp?

Try using function (#') instead of quote ('), i.e.

(defun make-point (x y)
     #'(lambda (msg)
        (cond ((equal msg 'x) x)
              ((equal msg 'y) y))))

(setq p (make-point 1 2))

When this is called, a closure, in which x is bound to 1 and y
is bound to 2, is returned and bound to p.

Then, (apply p '(x)) => 1 and (apply p '(y)) => 2.

If instead you define your function with quote (') instead of function (#'),
closure creation doesn't happen and x and y are not bound to anything.
So what apply gets is then
(LAMBDA (MSG) (COND ((EQUAL MSG (QUOTE X)) X) ((EQUAL MSG (QUOTE Y)) Y)))
and even though it is lenient and accepts this in lieu of a closure,  x and y
are
not bound to anything as no closure was created..

>                   Miki Tebeka

--
Le Hibou (mo bheachd fh�in: my own opinion)
"it's just that in C++ and the like, you don't trust _anybody_,
and in CLOS you basically trust everybody.  the practical result
is that thieves and bums use C++ and nice people use CLOS."
 -- Erik Naggum
From: Espen Vestre
Subject: Re: Lambda expressions
Date: 
Message-ID: <w61zn3reu8.fsf@gromit.nextel.no>
Michael Tebeka <·······@iil.intel.com> writes:

> But when I try in Lisp:
> (defun make-point (x y)
>      '(lambda (msg)

just remove the quote :-)

-- 

  (regards
     (espen vestre))
From: Barry Margolin
Subject: Re: Lambda expressions
Date: 
Message-ID: <lwZ32.10$zQ5.339635@burlma1-snr1.gtei.net>
In article <··············@gromit.nextel.no>,
Espen Vestre  <··@nextel.no> wrote:
>Michael Tebeka <·······@iil.intel.com> writes:
>
>> But when I try in Lisp:
>> (defun make-point (x y)
>>      '(lambda (msg)
>
>just remove the quote :-)

Or if your CL implementation doesn't have the LAMBDA macro (it wasn't in
CLTL, but it's in ANSI CL), use #':

(defun make-point (x y)
  #'(lambda (msg) ...))

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
From: Rainer Joswig
Subject: Re: Lambda expressions
Date: 
Message-ID: <joswig-1611981959150001@pbg3.lavielle.com>
In article <···················@burlma1-snr1.gtei.net>, Barry Margolin
<······@bbnplanet.com> wrote:

> In article <··············@gromit.nextel.no>,
> Espen Vestre  <··@nextel.no> wrote:
> >Michael Tebeka <·······@iil.intel.com> writes:
> >
> >> But when I try in Lisp:
> >> (defun make-point (x y)
> >>      '(lambda (msg)
> >
> >just remove the quote :-)
> 
> Or if your CL implementation doesn't have the LAMBDA macro (it wasn't in
> CLTL, but it's in ANSI CL), use #':
> 
> (defun make-point (x y)
>   #'(lambda (msg) ...))

Or implement the LAMBDA macro yourself?

Shouldn't be that hard...

-- 
http://www.lavielle.com/~joswig
From: Marco Antoniotti
Subject: Re: Lambda expressions
Date: 
Message-ID: <lwlnlafy7p.fsf@copernico.parades.rm.cnr.it>
······@lavielle.com (Rainer Joswig) writes:

> In article <···················@burlma1-snr1.gtei.net>, Barry Margolin
> <······@bbnplanet.com> wrote:
> 
> > In article <··············@gromit.nextel.no>,
> > Espen Vestre  <··@nextel.no> wrote:
> > >Michael Tebeka <·······@iil.intel.com> writes:
> > >
> > >> But when I try in Lisp:
> > >> (defun make-point (x y)
> > >>      '(lambda (msg)
> > >
> > >just remove the quote :-)
> > 
> > Or if your CL implementation doesn't have the LAMBDA macro (it wasn't in
> > CLTL, but it's in ANSI CL), use #':
> > 
> > (defun make-point (x y)
> >   #'(lambda (msg) ...))
> 
> Or implement the LAMBDA macro yourself?
> 

Why the LAMBDA macro (or special form) was left out of CLtL1 is one of
the mysteries of the universe.

Cheers

-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - (0)6 - 68 10 03 16, fax. +39 - (0)6 - 68 80 79 26
http://www.parades.rm.cnr.it
From: Barry Margolin
Subject: Re: Lambda expressions
Date: 
Message-ID: <E6h42.65$zQ5.821657@burlma1-snr1.gtei.net>
In article <··············@copernico.parades.rm.cnr.it>,
Marco Antoniotti  <·······@copernico.parades.rm.cnr.it> wrote:
>Why the LAMBDA macro (or special form) was left out of CLtL1 is one of
>the mysteries of the universe.

The Lisps that CL was derived from didn't have such a macro, AFAIK.  Most
of Common Lisp came from features in Maclisp and Lisp Machine Lisp.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
From: Marco Antoniotti
Subject: Re: Lambda expressions
Date: 
Message-ID: <lwogq5l904.fsf@copernico.parades.rm.cnr.it>
Barry Margolin <······@bbnplanet.com> writes:

> In article <··············@copernico.parades.rm.cnr.it>,
> Marco Antoniotti  <·······@copernico.parades.rm.cnr.it> wrote:
> >Why the LAMBDA macro (or special form) was left out of CLtL1 is one of
> >the mysteries of the universe.
> 
> The Lisps that CL was derived from didn't have such a macro, AFAIK.  Most
> of Common Lisp came from features in Maclisp and Lisp Machine Lisp.
> 

Yes, but Scheme was already around and the benefit (and obvious
special state) of LAMBDA were already known.

-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - (0)6 - 68 10 03 16, fax. +39 - (0)6 - 68 80 79 26
http://www.parades.rm.cnr.it
From: Kent M Pitman
Subject: Re: Lambda expressions
Date: 
Message-ID: <sfwvhju0xnq.fsf@world.std.com>
Marco Antoniotti <·······@copernico.parades.rm.cnr.it> writes:

> Barry Margolin <······@bbnplanet.com> writes:
> 
> > In article <··············@copernico.parades.rm.cnr.it>,
> > Marco Antoniotti  <·······@copernico.parades.rm.cnr.it> wrote:
> > >Why the LAMBDA macro (or special form) was left out of CLtL1 is one of
> > >the mysteries of the universe.

Hindsight is always 20/20.

> > The Lisps that CL was derived from didn't have such a macro, AFAIK.  Most
> > of Common Lisp came from features in Maclisp and Lisp Machine Lisp.
> > 
> 
> Yes, but Scheme was already around and the benefit (and obvious
> special state) of LAMBDA were already known.

It was left out because

(a) some people felt lambda expressions were "more primitive" and that it
    was better for lambda to be a "special token" than a macro.
    some people felt having it be a macro in addition to being a special
    token was redundant, sort of in the same way that setf and setq are
    sort of redundant (well, in a one-sided way).

(b) some people felt you should know how to write the one-line macro and
    that it therefore didn't belong in the language.

In fact, (b) didn't work because multiple people tried to define it
and were clobbering each others' PROBABLY-same definitions.  (Note I
say only probably the same since some people might have been old APL
hackers or something like that and trying to do something
different... or am I misremembering that APL had a lambda operator?
Anyway, the point is that the letter lambda IS used elsewhere...)

It wasn't until later when ANSI CL spelled out restrictions on
packages that disallowed users writing the macro (since you couldn't
redefine functions in the CL-USER package) that (b) didn't work any
more. At that point, the designers "fixed" things.

Incidentally, at the time of CLTL1, Lisp Machines ruled much of the
land and they all had a lambda character, which a lot of us used as a
macro to expand into (FUNCTION (LAMBDA...)) without clobbering the
ASCII name LAMBDA.