From: Sushovan
Subject: newbie - partially evaluate expression
Date: 
Message-ID: <3ceda350-a2ab-4654-adc7-e52e1495b511@r41g2000prr.googlegroups.com>
Hi,
  I am trying to write a function that generates a lisp expression
ready to pass to eval like this:
  (defun foo (x y) '(expt x (- y 1)))
However, when I run this with
  (foo 'z 24)
I get:
  (EXPT X (- Y 1))

What do I need to change in foo to make it evaluate to
  (EXPT Z 23)

Any help would be appreciated. Thanks.
Sushovan

From: Pascal Costanza
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <6u9ik7Fe5cscU1@mid.individual.net>
Sushovan wrote:
> Hi,
>   I am trying to write a function that generates a lisp expression
> ready to pass to eval like this:
>   (defun foo (x y) '(expt x (- y 1)))
> However, when I run this with
>   (foo 'z 24)
> I get:
>   (EXPT X (- Y 1))
> 
> What do I need to change in foo to make it evaluate to
>   (EXPT Z 23)
> 
> Any help would be appreciated. Thanks.

(defun foo (x y) `(expt ,x ,(- y 1)))


Pascal

-- 
ELS'09: http://www.european-lisp-symposium.org/
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Sushovan
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <8ce4360d-403a-4bf5-9b24-432c4c9f1da9@i24g2000prf.googlegroups.com>
On Jan 27, 4:11 pm, Pascal Costanza <····@p-cos.net> wrote:
> Sushovan wrote:
> > Hi,
> >   I am trying to write a function that generates a lisp expression
> > ready to pass to eval like this:
> >   (defun foo (x y) '(expt x (- y 1)))
> > However, when I run this with
> >   (foo 'z 24)
> > I get:
> >   (EXPT X (- Y 1))
>
> > What do I need to change in foo to make it evaluate to
> >   (EXPT Z 23)
>
> > Any help would be appreciated. Thanks.
>
> (defun foo (x y) `(expt ,x ,(- y 1)))
>
> Pascal
>
> --
> ELS'09:http://www.european-lisp-symposium.org/
> My website:http://p-cos.net
> Common Lisp Document Repository:http://cdr.eurolisp.org
> Closer to MOP & ContextL:http://common-lisp.net/project/closer/

Thank you so very much. Works perfectly.
From: Thomas A. Russ
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <ymivds0qm7f.fsf@blackcat.isi.edu>
Pascal Costanza <··@p-cos.net> writes:

> Sushovan wrote:
> > Hi,
> >   I am trying to write a function that generates a lisp expression
> > ready to pass to eval like this:
> >   (defun foo (x y) '(expt x (- y 1)))
> > However, when I run this with
> >   (foo 'z 24)
> > I get:
> >   (EXPT X (- Y 1))
> > What do I need to change in foo to make it evaluate to
> >   (EXPT Z 23)
> > Any help would be appreciated. Thanks.
> 
> (defun foo (x y) `(expt ,x ,(- y 1)))

A more flexible approach would be to use the built-in SUBLIS function to
do the substitutions for you.  It should work well as long as you don't
have any binding forms or constants in the expression tree.
   
  (sublis '((x . z) (y . 23)) '(expt x (- y 1)))

  ==>  (EXPT Z (- 23 1))



But one is compelled by decades of lisp group tradition to ask:
  "Why do you want to use EVAL in the first place?"

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal J. Bourguignon
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <877i4g2mdj.fsf@galatea.local>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Pascal Costanza <··@p-cos.net> writes:
>
>> Sushovan wrote:
>> > Hi,
>> >   I am trying to write a function that generates a lisp expression
>> > ready to pass to eval like this:
>> >   (defun foo (x y) '(expt x (- y 1)))
>> > However, when I run this with
>> >   (foo 'z 24)
>> > I get:
>> >   (EXPT X (- Y 1))
>> > What do I need to change in foo to make it evaluate to
>> >   (EXPT Z 23)
>> > Any help would be appreciated. Thanks.
>> 
>> (defun foo (x y) `(expt ,x ,(- y 1)))
>
> A more flexible approach would be to use the built-in SUBLIS function to
> do the substitutions for you.  It should work well as long as you don't
> have any binding forms or constants in the expression tree.
>    
>   (sublis '((x . z) (y . 23)) '(expt x (- y 1)))
>
>   ==>  (EXPT Z (- 23 1))
>
>
>
> But one is compelled by decades of lisp group tradition to ask:
>   "Why do you want to use EVAL in the first place?"

Indeed.  You should probably better provide a lambda form that can be compiled:

    (defun foo (x y)
      `(lambda ()
         (let ((x ,x)
               (y ,y))
             (expt x (1- y)))))


Then you can do:

 (funcall (compile nil (foo 2 3)))

and avoid the so dreaded EVAL.

-- 
__Pascal Bourguignon__
From: Sushovan
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <bb9ec836-b757-49d6-89fd-1f50452fb16f@f40g2000pri.googlegroups.com>
On Jan 27, 7:30 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> ····@sevak.isi.edu (Thomas A. Russ) writes:
>
>
>
>
>
> > Pascal Costanza <····@p-cos.net> writes:
>
> >> Sushovan wrote:
> >> > Hi,
> >> >   I am trying to write a function that generates a lisp expression
> >> > ready to pass to eval like this:
> >> >   (defun foo (x y) '(expt x (- y 1)))
> >> > However, when I run this with
> >> >   (foo 'z 24)
> >> > I get:
> >> >   (EXPT X (- Y 1))
> >> > What do I need to change in foo to make it evaluate to
> >> >   (EXPT Z 23)
> >> > Any help would be appreciated. Thanks.
>
> >> (defun foo (x y) `(expt ,x ,(- y 1)))
>
> > A more flexible approach would be to use the built-in SUBLIS function to
> > do the substitutions for you.  It should work well as long as you don't
> > have any binding forms or constants in the expression tree.
>
> >   (sublis '((x . z) (y . 23)) '(expt x (- y 1)))
>
> >   ==>  (EXPT Z (- 23 1))
>
> > But one is compelled by decades of lisp group tradition to ask:
> >   "Why do you want to use EVAL in the first place?"
>
> Indeed.  You should probably better provide a lambda form that can be compiled:
>
>     (defun foo (x y)
>       `(lambda ()
>          (let ((x ,x)
>                (y ,y))
>              (expt x (1- y)))))
>
> Then you can do:
>
>  (funcall (compile nil (foo 2 3)))
>
> and avoid the so dreaded EVAL.
>
> --
> __Pascal Bourguignon__- Hide quoted text -
>
> - Show quoted text -

The problem statement says.. Write a lisp program to generate an
expression A that when eval-ed would produce output B. I am not
actually using eval in my code.

Sushovan.
From: D Herring
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <497fe7a4$0$3337$6e1ede2f@read.cnntp.org>
Sushovan wrote:

> The problem statement says.. Write a lisp program to generate an
> expression A that when eval-ed would produce output B. I am not
> actually using eval in my code.


FWIW, politeness dictates that you always indicate when your question 
is part of your homework.

- Daniel
From: Matthew D Swank
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <39Qfl.35075$E%6.21129@newsfe04.iad>
On Wed, 28 Jan 2009 03:30:16 +0100, Pascal J. Bourguignon wrote:

> ···@sevak.isi.edu (Thomas A. Russ) writes:
> 
>> Pascal Costanza <··@p-cos.net> writes:
>>
>>> Sushovan wrote:
>>> > Hi,
>>> >   I am trying to write a function that generates a lisp expression
>>> > ready to pass to eval like this:
>>> >   (defun foo (x y) '(expt x (- y 1)))
>>> > However, when I run this with
>>> >   (foo 'z 24)
>>> > I get:
>>> >   (EXPT X (- Y 1))
>>> > What do I need to change in foo to make it evaluate to
>>> >   (EXPT Z 23)
>>> > Any help would be appreciated. Thanks.
>>> 
>>> (defun foo (x y) `(expt ,x ,(- y 1)))
>>
>> A more flexible approach would be to use the built-in SUBLIS function
>> to do the substitutions for you.  It should work well as long as you
>> don't have any binding forms or constants in the expression tree.
>>    
>>   (sublis '((x . z) (y . 23)) '(expt x (- y 1)))
>>
>>   ==>  (EXPT Z (- 23 1))
>>
>>
>>
>> But one is compelled by decades of lisp group tradition to ask:
>>   "Why do you want to use EVAL in the first place?"
> 
> Indeed.  You should probably better provide a lambda form that can be
> compiled:
> 
>     (defun foo (x y)
>       `(lambda ()
>          (let ((x ,x)
>                (y ,y))
>              (expt x (1- y)))))
> 
> 
> Then you can do:
> 
>  (funcall (compile nil (foo 2 3)))
> 
> and avoid the so dreaded EVAL.

Why is this necessarily better than eval (especially if you funcall the 
compiled function only once)?

Matt

-- 
Wrinkles should merely indicate where smiles have been.
		-- Mark Twain
From: Pascal J. Bourguignon
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <87zlhb258i.fsf@galatea.local>
Matthew D Swank <··················@gmail.com> writes:

> On Wed, 28 Jan 2009 03:30:16 +0100, Pascal J. Bourguignon wrote:
>> Indeed.  You should probably better provide a lambda form that can be
>> compiled:
>> 
>>     (defun foo (x y)
>>       `(lambda ()
>>          (let ((x ,x)
>>                (y ,y))
>>              (expt x (1- y)))))
>> 
>> 
>> Then you can do:
>> 
>>  (funcall (compile nil (foo 2 3)))
>> 
>> and avoid the so dreaded EVAL.
>
> Why is this necessarily better than eval (especially if you funcall the 
> compiled function only once)?

Well, technically I didn't say that FUNCALL COMPILE LAMBDA was better
than eval.  I said that you'd better use it to be able to avoid EVAL.
I have no a-priori about whether you should avoid EVAL or not.

Perhaps you've noticed it's not the first time I mention the
equivalence between EVAL and FUNCALL COMPILE LAMBDA.  Perhaps this
constitutes a hint about what I really think about using EVAL (when
it's required)?

-- 
__Pascal Bourguignon__
From: Matthew D. Swank
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <ed2bb754-7e80-426d-8747-a814c2bbb88c@v39g2000pro.googlegroups.com>
On Jan 28, 2:40 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Perhaps you've noticed it's not the first time I mention the
> equivalence between EVAL and FUNCALL COMPILE LAMBDA.  

I'm lucky if I notice what day of the week it is.  Sometimes the only
reason I know I've been up all night is the cats suddenly want fed.

I wish wish you luck in all you comp.lang.lisp pedagogical exercises.
Now if you could only give an example eval/compile equivalency that
included a way to erase someone's hard drive.

Matt
From: Chaitanya Gupta
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <glp7nq$5k3$1@news.motzarella.org>
Pascal J. Bourguignon wrote:
> 
> Indeed.  You should probably better provide a lambda form that can be compiled:
> 
>     (defun foo (x y)
>       `(lambda ()
>          (let ((x ,x)
>                (y ,y))
>              (expt x (1- y)))))
> 
> 
> Then you can do:
> 
>  (funcall (compile nil (foo 2 3)))
> 

Why not just

(defun foo (x y)
   (lambda () (expt x (1- y))))

And then

   (compile 'foo)

and finally

   (funcall (foo 2 3))

Won't (foo 2 3) return a compiled closure?

Chaitanya
From: Pascal J. Bourguignon
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <7cfxj3kbut.fsf@pbourguignon.anevia.com>
Chaitanya Gupta <····@chaitanyagupta.com> writes:

> Pascal J. Bourguignon wrote:
>> Indeed.  You should probably better provide a lambda form that can
>> be compiled:
>>     (defun foo (x y)
>>       `(lambda ()
>>          (let ((x ,x)
>>                (y ,y))
>>              (expt x (1- y)))))
>> Then you can do:
>>  (funcall (compile nil (foo 2 3)))
>> 
>
> Why not just
>
> (defun foo (x y)
>   (lambda () (expt x (1- y))))
>
> And then
>
>   (compile 'foo)
>
> and finally
>
>   (funcall (foo 2 3))
>
> Won't (foo 2 3) return a compiled closure?

Yes.  But this is not what is specified, and it doesn't give the same
results:


C/USER[1]> (defun foo (x y)
              (lambda () (expt x (1- y))))
FOO
C/USER[2]> (foo 2 3)
#<FUNCTION :LAMBDA NIL (EXPT X (1- Y))>
C/USER[3]> (eval *)
#<FUNCTION :LAMBDA NIL (EXPT X (1- Y))>



C/USER[4]>  (defun foo (x y)
              `(expt ,x (1- ,y)))
FOO
C/USER[5]> (foo 2 3)
(EXPT 2 (1- 3))
C/USER[6]> (eval *)
4


And presumably, foo will build (or read) different expressions, it
won't always be the same I'd guess.

-- 
__Pascal Bourguignon__
From: Mark Wooding
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <87r62np1y0.fsf.mdw@metalzone.distorted.org.uk>
···@informatimago.com (Pascal J. Bourguignon) writes:

> Indeed.  You should probably better provide a lambda form that can be
> compiled:
>
>     (defun foo (x y)
>       `(lambda ()
>          (let ((x ,x)
>                (y ,y))
>              (expt x (1- y)))))
>
>
> Then you can do:
>
>  (funcall (compile nil (foo 2 3)))

There's no need to invoke the compiler here.

        (funcall (coerce (foo 2 3) 'function))

is sufficient.  I didn't see a performance requirement mentioned in the
thread.  (I think SBCL, for example, will exercise the compiler anyway
in COERCE.)

-- [mdw]
From: William James
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <glond80qcb@enews4.newsguy.com>
Sushovan wrote:

> Hi,
>   I am trying to write a function that generates a lisp expression
> ready to pass to eval like this:
>   (defun foo (x y) '(expt x (- y 1)))
> However, when I run this with
>   (foo 'z 24)
> I get:
>   (EXPT X (- Y 1))
> 
> What do I need to change in foo to make it evaluate to
>   (EXPT Z 23)
> 
> Any help would be appreciated. Thanks.
> Sushovan

def foo x,y
  "expt( #{ x }, #{ y-1 } )"
end
    ==>nil
foo :z, 24
    ==>"expt( z, 23 )"
From: Marco Antoniotti
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <d3bbcc88-d5ab-41b4-8eee-45a121dc5dbe@r41g2000prr.googlegroups.com>
On Jan 28, 5:36 am, "William James" <·········@yahoo.com> wrote:
> Sushovan wrote:
> > Hi,
> >   I am trying to write a function that generates a lisp expression
> > ready to pass to eval like this:
> >   (defun foo (x y) '(expt x (- y 1)))
> > However, when I run this with
> >   (foo 'z 24)
> > I get:
> >   (EXPT X (- Y 1))
>
> > What do I need to change in foo to make it evaluate to
> >   (EXPT Z 23)
>
> > Any help would be appreciated. Thanks.
> > Sushovan
>
> def foo x,y
>   "expt( #{ x }, #{ y-1 } )"
> end
>     ==>nil
> foo :z, 24
>     ==>"expt( z, 23 )"

Now you are making sense.   It is obvious that Ruby gets uglier and
uglier when trying to do simple things :)

Cheers
--
Marco Antoniotti
www.european-lisp-symposium.org
From: ·········@yahoo.com
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <b19fd32a-4559-40dd-953a-f8b7bbf90b8a@q36g2000vbn.googlegroups.com>
On Jan 27, 4:59 pm, Sushovan <··········@gmail.com> wrote:
> Hi,
>   I am trying to write a function that generates a lisp expression
> ready to pass to eval like this:
>   (defun foo (x y) '(expt x (- y 1)))
> However, when I run this with
>   (foo 'z 24)
> I get:
>   (EXPT X (- Y 1))
>
> What do I need to change in foo to make it evaluate to
>   (EXPT Z 23)
>
> Any help would be appreciated. Thanks.
> Sushovan

OCaml:

# let mul x y = x * y;;
val mul : int -> int -> int = <fun>
# mul 7 8;;
- : int = 56
# let mul_55 = mul 55;;
val mul_55 : int -> int = <fun>
# mul_55 3;;
- : int = 165
From: ······@corporate-world.lisp.de
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <0af0e8aa-ec06-43a5-a502-5d3d8ae6eba5@v5g2000pre.googlegroups.com>
On 28 Jan., 19:14, ·········@yahoo.com wrote:
> On Jan 27, 4:59 pm, Sushovan <··········@gmail.com> wrote:
>
> > Hi,
> >   I am trying to write a function that generates a lisp expression
> > ready to pass to eval like this:
> >   (defun foo (x y) '(expt x (- y 1)))
> > However, when I run this with
> >   (foo 'z 24)
> > I get:
> >   (EXPT X (- Y 1))
>
> > What do I need to change in foo to make it evaluate to
> >   (EXPT Z 23)
>
> > Any help would be appreciated. Thanks.
> > Sushovan
>
> OCaml:
>
> # let mul x y = x * y;;
> val mul : int -> int -> int = <fun>
> # mul 7 8;;
> - : int = 56
> # let mul_55 = mul 55;;
> val mul_55 : int -> int = <fun>
> # mul_55 3;;
> - : int = 165

I don't think that was the requested task.

The task is to write a function that generates an expression.
You wrote a function that computes a multiplication
and then a curried version.

You failed to write a function that generates an expression.
From: Marco Antoniotti
Subject: Re: newbie - partially evaluate expression
Date: 
Message-ID: <ce5696fb-34ae-436e-b7dc-ad39e4bcdd1a@y23g2000pre.googlegroups.com>
On Jan 28, 7:14 pm, ·········@yahoo.com wrote:
> On Jan 27, 4:59 pm, Sushovan <··········@gmail.com> wrote:
>
> > Hi,
> >   I am trying to write a function that generates a lisp expression
> > ready to pass to eval like this:
> >   (defun foo (x y) '(expt x (- y 1)))
> > However, when I run this with
> >   (foo 'z 24)
> > I get:
> >   (EXPT X (- Y 1))
>
> > What do I need to change in foo to make it evaluate to
> >   (EXPT Z 23)
>
> > Any help would be appreciated. Thanks.
> > Sushovan
>
> OCaml:
>
> # let mul x y = x * y;;
> val mul : int -> int -> int = <fun>
> # mul 7 8;;
> - : int = 56
> # let mul_55 = mul 55;;
> val mul_55 : int -> int = <fun>
> # mul_55 3;;
> - : int = 165

This OCaml is the following CL (sans types)

   (defun mul (x y) (* x y))

   (defsubst mul_55 (curry 'mul 55))

   (mul_55 3)

where DEFSUBST and CURRY are just a macro away (or other appropriately
named macros, whose existence in ALEXANDRIA I now conjecture :) )

But that is not wat the OP asked.  He asked for something returning a
partially evaluated *expression*.

As for CURRY, let's do a preventive googling with "currying in Ruby"
just for fun.... :)

Cheers
--
Marco