tags:

views:

615

answers:

6
+2  Q: 

EVAL in SCHEME

Peter Norvig in PAIP says:
" in modern lisps...eval is used less often (in fact, in Scheme there is no eval at all)" " if you find yourself using eval, you are probably doing the wrong thing".

What are some of the ways to circumvent using eval in scheme? Arent there case where eval is absolutely necessary?

+1  A: 

First, PAIP is written for Common Lisp, not Scheme, so I don't know that he'd say the same thing. CL macros do much the same thing as eval, although at compile time instead of run time, and there's other things you could do. If you'd show me an example of using eval in Common Lisp, I could try to come up with other methods of doing the same thing.

I'm not a Scheme programmer. I can only speak from Norvig's perspective, as a Common Lisp programmer. I don't think he was talking about Scheme, and I don't know if he knew or knows Scheme particularly well.

Second, Norvig says "you are probably doing the wrong thing" rather than "you're doing the wrong thing". This implies that, for all he knows, there's times when eval is the correct thing to use. In a language like C, I'd say the same thing about goto, although they're quite useful in some restricted circumstances, but most goto use is by people who don't know any better.

David Thornley
"I dont know if he knows Scheme particularly well". This statement is quite unbelievable. "there's times when eval is the correct thing to use". That was my second question. As practicing programmers, have you used eval?
kunjaan
Obviously, Norvig is extremely good at Common Lisp, having written a truly excellent book about it. That doesn't apply to Scheme. Would Stroustrup necessarily be a good authority on the best way to do something in Java or Objective-C? That family of languages is no more disparate than the Lisp family including Common Lisp and Scheme.
David Thornley
hmmm he does implement scheme in lisp..oh well i dont know . but i would assume he lies at the "knows particularly well" spectrum.
kunjaan
A: 

You could write a scheme interpreter in scheme. Such is certainly possible, but it is not practical.

Granted, this is a general answer, as I have no used scheme, but it may help you nonetheless. :)

Stefan Kendall
This is no where near the answer I wanted. : )
kunjaan
A: 

He's wrong. Of course there is eval in Scheme.

newacct
Which scheme? I don't think r4rs required eval to be present at all.
Aaron
R5RShttp://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.5
newacct
+4  A: 

There are cases where eval is necessary, but they always involve advanced programs that do things like dynamically loading some code (eg, a servlet in a web server). As for a way to "circumvent" using it -- that depends on the actual problem you're trying to solve, there's no magic solution to avoiding eval except for ... eval.

(BTW, my guess is that PAIP was written a long time ago, before eval was added to the Scheme Report.)

Eli Barzilay
I am sorry I couldn't be more specific about what i wanted in the question. But whats interesting is that I always thought eval was always an integral part of Scheme. Anyway I hope I get to see more usage and 'abuses' of eval in CS G111. :)
kunjaan
Yes, `eval` *is* an integral part of Scheme, but it is only really needed in advanced cases. The rule of thumb is: if you don't know the technical reasons that require you to use `eval`, then you *don't* need it. (And this is a serious answer!)In any case, yes, you'll have lots of `eval`ing in CSG111 if you're going to take it, and hopefully if you remember, ask me about it.
Eli Barzilay
A: 

One use I've seen for 'eval' in scripting environments is to parameterize some code with runtime values. for instance, in psuedo-C:

param = read_integer();
fn = eval("int lambda(int x) { 
            int param = " + to_string(param) + "; 
            return param*x; }");

I hope you find that really ugly. String pasting to create code at runtime? Ick. In Scheme and other lexically scoped Lisps, you can make parameterized functions without using eval.

(define make-my-fn
  (lambda (param)
    (lambda (x)  (* param x)))

(let* ([ param  (read-integer) ]
       [ fn     (make-my-fn param ])
  ;; etc.
 )

Like was mentioned, dynamic code loading and such still need eval, but parameterized code and code composition can be produced with first class functions.

Aaron
+1  A: 

You'll need eval in some very rare case. The case that comes into mind first is when you'll build program with a program and then execute it. This happens mainly with genetic algorithm for example. In this case you build a lot a randomized programs that you'll need to execute. Having eval in conjunction with code being data make lisp the easiest programming language to do genetic algorithm.

Having these properties comes at a great cost (in term of speed and size of your program) because you'll remove all possibility to do compile time optimization on the code that will be evaled and you must keep the full interpreter in your resulting binary.

As a result it is considered poor design to use eval when it can be avoided.

vaab