You should be automatically redirected . If not, visit
http://newlisper.wordpress.com
and update your bookmarks.

17/05/2006

The lesser of two evals

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!

4 Comments:

At 02:15, Anonymous m i c h a e l said...

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

 
At 17:52, Blogger don Lucio said...

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.

 
At 18:14, Blogger newlisper said...

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

 
At 19:03, Blogger sarken said...

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

 

Post a Comment

Links to this post:

Create a Link

<< Home