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
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/
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.
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
···@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__
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.
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
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
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__
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
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
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__
···@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]
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 )"
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
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
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.
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