<div dir="ltr">Sorry, couple typos: mapply = map and l = lst.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Aug 9, 2014 at 10:10 AM, Sean Kanaley <span dir="ltr"><<a href="mailto:skanaley@gmail.com" target="_blank">skanaley@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">There's a simple enough example I think: apply with keyword parameters. Apply has that built-in..sort of...but the list is supposed to be provided inline to APPLY, seemingly requiring apply to be applied (e.g. (apply apply (cons *thefunctiontoapply* params))), except keywords can't be expressions anyway. I have a bunch of card definitions for Dominion with many defaults:<div>
<br></div><div>(struct card (... actions buys ...))</div><div><br></div><div>(define (make-card ... #:actions actions #:buy buys ...)</div><div> ...)</div><div><br></div><div>The non-keyword-requiring way is</div><div><br>
</div><div>(define CARDS</div><div> (map (\ (lst) (apply make-card lst))</div><div> '([... 1 1 ...]</div><div> [... 2 0 ...])))</div><div><br></div><div>The keyword way is</div><div><br></div><div>
;ns is namespace</div><div>(mapply (\ (lst) (eval `(apply make-card ',(car l) ,@(cdr l)) ns))<br></div><div> '([(...) #:actions 1 #:buys 1 ...]))</div><div><br></div><div>Short of making a big macro to take some exact format and get it to work with keywords and all that, eval seems to be just what is needed.</div>
</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Aug 9, 2014 at 8:27 AM, Neil Van Dyke <span dir="ltr"><<a href="mailto:neil@neilvandyke.org" target="_blank">neil@neilvandyke.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sounds like a good rule of thumb. Two suggestions to add:<br>
<br>
* Maybe there could also be a second rule of thumb, like, "If you need arbitrary Racket expressions, then consider whether you can do it with one of the following patterns: [list some patterns involving combinations of syntax extension, custom #lang, dynamic requires, considering whether config files can actually be compile-time #lang, etc.]" This is less important than the first rule of thumb.<br>
<br>
* When we list typical newbie cases that don't actually require "eval", we can expect that newbie will immediately want an example of the non-"eval" way to do that typical thing. At the very least, an example showing, say, good use of hashes in parameters, with a sensible thin abstraction layer over it, for that case. These examples would be tedious to work through and write up well, but someday some knowledgeable and benevolent person will do it. (I am not this person. Book royalties aren't enough to ease the pain. I'd rather that newbies just never heard of "eval" or were scared away from it, rather than having to talk them down off the ledge all the time.)<br>
<br>
Neil V.<br>
<br>
<br>
Eli Barzilay wrote at 08/09/2014 07:31 AM:<div><div><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I think that I ran into a nice way to discourage eval except when<br>
needed: the rule of thumb is to only use eval when you need to evaluate<br>
any arbitrary (Racket) expression. It sounds simplistic but it covers<br>
lots of cases that make newbies run to eval:<br>
<br>
* I just want to get the value of a variable whose name is held in x<br>
<br>
* More common: I have a symbol/string that names a function, I just need<br>
to call that function<br>
<br>
* I need to keep an update-able mapping from names to values, just like<br>
what the toplevel does<br>
<br>
* I want to let people write an arithmetic expression instead of a<br>
number<br>
<br>
In such cases questions like "do you need allow calling all functions",<br>
and "do you need to handle lambda expressions" have negative answers.<br>
<br>
<br>
<br>
On Sun, Jul 27, 2014 at 4:16 PM, Neil Van Dyke <<a href="mailto:neil@neilvandyke.org" target="_blank">neil@neilvandyke.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Maybe there should be a periodic public service announcement about not using<br>
"eval". This time I will communicate in FAQ format:<br>
<br>
Q: How do I use eval?<br>
A: Don't use eval.<br>
<br>
Q: But don't so many academic books feature eval prominently, so doesn't<br>
that mean I should use try to eval?<br>
A: Those books use eval for pedagogic reasons, or because the author is<br>
enamored of some theoretical appeal of eval, or because the author wants to<br>
watch the world burn. Don't use eval.<br>
<br>
Q: But, but, but, I am just starting to learn, and eval seems to do what I<br>
need.<br>
A: Eval is almost certainly not what you want. Learn how to use the other<br>
basics effectively. Don't use eval.<br>
<br>
Q: I now am very comfortable with the language, I am aware that I should<br>
avoid eval in almost all cases, and I can tell you why eval is actually the<br>
right thing in this highly unusual case.<br>
A: Cool, that's why eval is there.<br>
<br>
Neil V.<br>
<br>
<br>
</blockquote></blockquote>
<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>