[racket] Use of map and eval to evaluate symbol in namespace

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Aug 9 07:31:03 EDT 2014

I think that I ran into a nice way to discourage eval except when
needed: the rule of thumb is to only use eval when you need to evaluate
any arbitrary (Racket) expression.  It sounds simplistic but it covers
lots of cases that make newbies run to eval:

* I just want to get the value of a variable whose name is held in x

* More common: I have a symbol/string that names a function, I just need
  to call that function

* I need to keep an update-able mapping from names to values, just like
  what the toplevel does

* I want to let people write an arithmetic expression instead of a
  number

In such cases questions like "do you need allow calling all functions",
and "do you need to handle lambda expressions" have negative answers.



On Sun, Jul 27, 2014 at 4:16 PM, Neil Van Dyke <neil at neilvandyke.org> wrote:
> Maybe there should be a periodic public service announcement about not using
> "eval".  This time I will communicate in FAQ format:
>
> Q: How do I use eval?
> A: Don't use eval.
>
> Q: But don't so many academic books feature eval prominently, so doesn't
> that mean I should use try to eval?
> A: Those books use eval for pedagogic reasons, or because the author is
> enamored of some theoretical appeal of eval, or because the author wants to
> watch the world burn.  Don't use eval.
>
> Q: But, but, but, I am just starting to learn, and eval seems to do what I
> need.
> A: Eval is almost certainly not what you want.  Learn how to use the other
> basics effectively.  Don't use eval.
>
> Q: I now am very comfortable with the language, I am aware that I should
> avoid eval in almost all cases, and I can tell you why eval is actually the
> right thing in this highly unusual case.
> A: Cool, that's why eval is there.
>
> Neil V.
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.