(archive 'newLISPer)

May 17, 2006

The lesser of two evals

Filed under: newLISP — newlisper @ 22:26
Tags:

newLISP has not one but two ‘eval’ functions, and they’re both useful. The first, eval, accepts an expression and evaluates it:

(set 'expr '(+ 1 2))
(eval expr)
;-> 3

The second, eval-string, accepts a string and evaluates it:

(set 'expr "(+ 1 2)")
(eval-string expr)
;-> 3

It’s as if eval-string lets you double-quote your Lisp code, whereas eval lets you single-quote it.

eval-string has some extra features. You can provide an additional argument that gets evaluated only if the string can’t be evaluated:

(set 'expr "(+ 1 2") ; missing parenthesis!
(eval-string expr "oops")
;-> oops

and it can even take another argument, to indicate the context for the evaluation.

Here’s an interesting use of this function. Say you knew of a useful web page where some handy newLISP was stored. You can do something like this:

(eval-string (get-url "http://www.newlisp.org/code/SentenceBoundary.lsp.txt"))
(url-to-sentences "http://newlisper.blogspot.com" 15000)

which fetches (and loads) some cool code from the newLISP site, then returns the newLISPer blog in the form of a list of sentences (with a timeout of 15 seconds). Yes, I know it’s not a good idea to load in and evaluate some code directly from a web page, so don’t try this at home! Neat, though, eh?

By the way, that code – the url-to-sentences function – might get more comment in the near future, when I get round to reading it. It’s written by the guys at kozoru, and it converts a web page to a list of sentences. If you should read this, guys, then thanks for the code, and good luck with your project!

Advertisements

4 Comments »

  1. >newlisper said: The lesser of two evals*grin*Wow! This was completely unexpected. Where did you find the time? I’m glad you did, though. Thanks!m i c h a e l

    Comment by m i c h a e l — May 18, 2006 @ 01:15 | Reply

  2. >BTW since the 2 last development versions of newLISP you can just say:(load “http://asite.com/aprog.lsp”)and it will combine ‘eval-string’ and ‘get-url’ into one function. Of course you loose the finer control ‘get-url’ gives you, e.g. for timeouts etc. Buts it’s a useful fature to load program code into machines distributed on a local network.

    Comment by don Lucio — May 18, 2006 @ 16:52 | Reply

  3. >newLISP is ahead of the game as ever! :-)

    Comment by newlisper — May 18, 2006 @ 17:14 | Reply

  4. >Thanks for the mention!You may want to be sure you’ve got the most recent version (2695) of SentenceBoundary. There was a small bug that would error out when the text of the web page ended with “. “Gord

    Comment by sarken — May 22, 2006 @ 18:03 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: