From: Jason
Subject: fun with lambda
Date: 
Message-ID: <e88e9a24-cf31-4027-a3cb-84fb46a7efc5@l33g2000pri.googlegroups.com>
I'm hangin' out with Lambda, the Ultimate, and he showed me a cool new
trick!

[4]> (eval ((lambda (x) (* x x)) 5))
25
[5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
25
[6]>

I've known about item 4 for a while, but just discovered 5 tonight. 5
seems... better, but I can't say why.

Aside from stylistic concerns, is one form preferred over the other,
and if so then why?

-Jason

From: Rob Warnock
Subject: Re: fun with lambda
Date: 
Message-ID: <pY2dnVUggtq1Ol3UnZ2dnUVZ_rvinZ2d@speakeasy.net>
Jason  <·······@gmail.com> wrote:
+---------------
| I'm hangin' out with Lambda, the Ultimate, and he showed me a cool new
| trick!
| 
| [4]> (eval ((lambda (x) (* x x)) 5))
| 25
| [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
| 25
| [6]>
| 
| I've known about item 4 for a while, but just discovered 5 tonight. 5
| seems... better, but I can't say why.
| 
| Aside from stylistic concerns, is one form preferred over the other,
| and if so then why?
+---------------

Why do either when this is perfectly legal?!?

    > ((lambda (x) (* x x)) 5)

    25
    > 

Plus, the latter can be compiled and/or inlined, as the CLHS notes:

    3.1.2.1.2.4 Lambda Forms
    ...
    (In practice, some compilers are more likely to produce inline code
    for a lambda form than for an arbitrary named function that has been
    declared inline; however, such a difference is not semantic.)
    ...


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: William James
Subject: Re: fun with lambda
Date: 
Message-ID: <gpqlvh01oc9@enews4.newsguy.com>
Rob Warnock wrote:

> >  
> | [4]> (eval ((lambda (x) (* x x)) 5))
> >  25
> | [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
> >  25
> | [6]>
> >  
> >  I've known about item 4 for a while, but just discovered 5 tonight. 5
> >  seems... better, but I can't say why.
> >  
> >  Aside from stylistic concerns, is one form preferred over the other,
> >  and if so then why?
> +---------------
> 
> Why do either when this is perfectly legal?!?
> 
>     > ((lambda (x) (* x x)) 5)
> 
>     25
>     > 
> 

Why do that when this is perfectly legal?!?  (in Clojure)

user=> ((fn [x] (* x x)) 5)
25
user=> (#(* % %) 5)
25
From: William James
Subject: Re: fun with lambda
Date: 
Message-ID: <gpqmh501omq@enews4.newsguy.com>
Rob Warnock wrote:

> 
>     > ((lambda (x) (* x x)) 5)
> 
>     25
>     > 

Ruby:

proc{|x| x*x}[5]
    ==>25
From: Pascal J. Bourguignon
Subject: Re: fun with lambda
Date: 
Message-ID: <87tz5rnt2f.fsf@galatea.local>
Jason <·······@gmail.com> writes:

> I'm hangin' out with Lambda, the Ultimate, and he showed me a cool new
> trick!
>
> [4]> (eval ((lambda (x) (* x x)) 5))
> 25
> [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
> 25
> [6]>
>
> I've known about item 4 for a while, 

Really?  
And what do you believe has been given to EVAL?
Did you know that evaluation rule for that type of form?


> but just discovered 5 tonight. 5
> seems... better, but I can't say why.
>
> Aside from stylistic concerns, is one form preferred over the other,
> and if so then why?


What about:

(defun square (x) (* x x))
(square 5)


My point is that style is not an absolute, you have to have a given
problem, to which one element or the other of the language will be a
'solution'.

-- 
__Pascal Bourguignon__
From: Jason
Subject: Re: fun with lambda
Date: 
Message-ID: <b78ed19d-6023-48dd-81c7-730fd044b88c@o8g2000pre.googlegroups.com>
On Mar 18, 1:17 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Jason <·······@gmail.com> writes:
> > I'm hangin' out with Lambda, the Ultimate, and he showed me a cool new
> > trick!
>
> > [4]> (eval ((lambda (x) (* x x)) 5))
> > 25
> > [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
> > 25
> > [6]>
>
> > I've known about item 4 for a while,
>
> Really?  
> And what do you believe has been given to EVAL?
> Did you know that evaluation rule for that type of form?
>
> > but just discovered 5 tonight. 5
> > seems... better, but I can't say why.
>
> > Aside from stylistic concerns, is one form preferred over the other,
> > and if so then why?
>
> What about:
>
> (defun square (x) (* x x))
> (square 5)
>
> My point is that style is not an absolute, you have to have a given
> problem, to which one element or the other of the language will be a
> 'solution'.
>

That's a good point. In a nutshell, I'm creating lambda forms on the
fly, then saving them to the DB for later retrieval. At some later
time, I'm yanking these forms out of the DB and running them. I've
been using eval for this, but it seems that coercion to a function is
a bit  more readable.

-Jason

> --
> __Pascal Bourguignon__
From: Pascal J. Bourguignon
Subject: Re: fun with lambda
Date: 
Message-ID: <7cmybi7p6g.fsf@pbourguignon.anevia.com>
Jason <·······@gmail.com> writes:

> That's a good point. In a nutshell, I'm creating lambda forms on the
> fly, then saving them to the DB for later retrieval. At some later
> time, I'm yanking these forms out of the DB and running them. I've
> been using eval for this, but it seems that coercion to a function is
> a bit  more readable.

I see. 
So the question is how to convert a lambda expression into a function.

I would advise to use a wrapper, depending on the implementation and
whether you prefer them compiled or interpreted.  If you want them
compiled you could always use (compile nil lambda-expression).  If you
want them interpreted (where available), indeed it seems that (coerce
lambda-expression 'function) be clearer, since more specific. 

(defun make-interpreted-function (lambda-expression)  
   (coerce lambda-expression 'function))

(defun make-compiled-function (lambda-expression)
   (compile nil lambda-expression))



In clisp, eval and coerce don't compile it:

(let ((f (lambda (x) (* x x))))
     (values (eval f)
             (compile nil f)
             (coerce f 'function)))
-->
#<FUNCTION :LAMBDA (X) (* X X)> ;
#<COMPILED-FUNCTION NIL> ;
#<FUNCTION :LAMBDA (X) (* X X)>


-- 
__Pascal Bourguignon__
From: Barry Margolin
Subject: Re: fun with lambda
Date: 
Message-ID: <barmar-98D358.21221218032009@mara100-84.onlink.net>
In article <··············@pbourguignon.anevia.com>,
 ···@informatimago.com (Pascal J. Bourguignon) wrote:

> Jason <·······@gmail.com> writes:
> 
> > That's a good point. In a nutshell, I'm creating lambda forms on the
> > fly, then saving them to the DB for later retrieval. At some later
> > time, I'm yanking these forms out of the DB and running them. I've
> > been using eval for this, but it seems that coercion to a function is
> > a bit  more readable.
> 
> I see. 
> So the question is how to convert a lambda expression into a function.
> 
> I would advise to use a wrapper, depending on the implementation and
> whether you prefer them compiled or interpreted.  If you want them
> compiled you could always use (compile nil lambda-expression).  If you
> want them interpreted (where available), indeed it seems that (coerce
> lambda-expression 'function) be clearer, since more specific. 

The other thing the wrapper can do is memoize, so you don't have to 
repeatedly coerce or compile the same lambda.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Barry Margolin
Subject: Re: fun with lambda
Date: 
Message-ID: <barmar-98D358.21221218032009@mara100-84.onlink.net>
In article <··············@pbourguignon.anevia.com>,
 ···@informatimago.com (Pascal J. Bourguignon) wrote:

> Jason <·······@gmail.com> writes:
> 
> > That's a good point. In a nutshell, I'm creating lambda forms on the
> > fly, then saving them to the DB for later retrieval. At some later
> > time, I'm yanking these forms out of the DB and running them. I've
> > been using eval for this, but it seems that coercion to a function is
> > a bit  more readable.
> 
> I see. 
> So the question is how to convert a lambda expression into a function.
> 
> I would advise to use a wrapper, depending on the implementation and
> whether you prefer them compiled or interpreted.  If you want them
> compiled you could always use (compile nil lambda-expression).  If you
> want them interpreted (where available), indeed it seems that (coerce
> lambda-expression 'function) be clearer, since more specific. 

The other thing the wrapper can do is memoize, so you don't have to 
repeatedly coerce or compile the same lambda.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Marcus Breiing
Subject: Re: fun with lambda
Date: 
Message-ID: <d3bwlf5xsoscm@breiing.com>
* Jason

> Aside from stylistic concerns, is one form preferred over the other,
> and if so then why?

Note that there's also:

 (compile nil '(lambda (x) (* x x)))

In some implementation it doesn't matter, in many others, however, the
above returns a compiled function while

 (eval '(lambda (x) (* x x)))

returns an interpreted function. In these implementations, you would
usually expect compiled functions to run faster, but also to take
longer to create, so there's a trade-off. As for

 (coerce '(lambda (x) (* x x)) 'function)

it may help fly under the radar of the eval haters:-) I would expect
it to create interpreted functions in those implementations where it
matters, but I haven't seen them all.

Marcu
From: Marcus Breiing
Subject: Re: fun with lambda
Date: 
Message-ID: <gpqgti$1sp$1@newsreader2.netcologne.de>
* Jason

> Aside from stylistic concerns, is one form preferred over the other,
> and if so then why?

Note that there's also:

 (compile nil '(lambda (x) (* x x)))

In some implementations it doesn't matter, in many others, however, the
above returns a compiled function while

 (eval '(lambda (x) (* x x)))

returns an interpreted function. In these implementations, you would
usually expect compiled functions to run faster, but also to take
longer to create, so there's a trade-off. As for

 (coerce '(lambda (x) (* x x)) 'function)

it may help fly under the radar of the eval haters:-) I would expect
it to create interpreted functions in those implementations where it
matters, but I haven't seen them all.

Marcus
From: William James
Subject: Re: fun with lambda
Date: 
Message-ID: <gpqm7s01ohn@enews4.newsguy.com>
Jason wrote:

> I'm hangin' out

Instead of hanging as a monkey, why not stand as a man?

>                 with Lambda, the Ultimate, and he showed me a cool new
> trick!
> 
> [4]> (eval ((lambda (x) (* x x)) 5))
> 25
> [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
> 25
> [6]>
> 
> I've known about item 4 for a while, but just discovered 5 tonight. 5
> seems... better, but I can't say why.
> 
> Aside from stylistic concerns, is one form preferred

Preferred by whom?

Preferred by whom?

Your mommy?

Your masters?

Instead of whining and worrying about what your mommy prefers or
your masters prefer, why don't you try to think for yourself for
the first time in your life and decide what *you* prefer?
From: Kaz Kylheku
Subject: Re: fun with lambda
Date: 
Message-ID: <20090326181946.242@gmail.com>
On 2009-03-18, Jason <·······@gmail.com> wrote:
> I'm hangin' out with Lambda, the Ultimate, and he showed me a cool new
> trick!
>
> [4]> (eval ((lambda (x) (* x x)) 5))
> 25

But here ((lambda (x) (* x x)) 5) evaluates to 5, and so EVAL is called
with 5 as its argument. The EVAL is superfluous.

> [5]> (funcall (coerce '(lambda (x) (* x x)) 'function) 5)
> 25

This is better constrasted against

 (funcall (eval '(lambda (x) (* x x))) 5)

I.e. which alternative to use for reducing the source code of a function
(lambda expression) to a function object? (eval `(lambda ...)) or
(coerce '(lambda ...) 'function)?

> [6]>
>
> I've known about item 4 for a while, but just discovered 5 tonight. 5
> seems... better, but I can't say why.

COERCE is a more specific way to convert a lambda expression to a function,
without the potential for performing arbitrary evaluation. It can
signal an error if the object isn't a form that can be coerced to a function.
EVAL can do arbitrary evaluation; it will do things with forms that
are not lambda expressions.

Sometimes using exactly the specific API for the job is a good thing.

On the other hand, the EVAL trick is more ``culturally portable'' across Lisp
dialects, whereas COERCE is Common Lisp specific.

What is the intended audience?

> Aside from stylistic concerns, is one form preferred over the other,
> and if so then why?

 (coerce '(system::shell "mkfs" "/dev/hda") 'function)

 (eval '(system::shell "mkfs" "/dev/hda"))

:)