How about (read) ?

Started by Bat, October 10, 2003, 08:52:29 AM

Previous topic - Next topic

Bat

How can you do reading of regular Lisp expressions, in the way that CL does it with (read) ?  Especially to read from files, its useful to be able to read a whole expression -enclosed in parentheses-. Maybe I've missed it somewhere ?  In fact, load must use such a function, so it cannot be difficult.



Also, there are some minor typos in the manual. For instance, in the random syntax definition of 'random', the scale and the offset appear to be swapped over.

eddier

#1
To read a whole file into one big string:



(read-file "filename")



Eddie

Lutz

#2
What 'load' does is more or less this:



(eval-string (read-file "afile.lsp"))



The only difference is that 'load' will stream the file and at the same time evaluate, while the above example reads the whole file first into a string, then evaluates it.



'load' can stream very large files without a memory impact, because it evaluates s-expressions as they flow in, compiling and evaluating on the fly.



newLISP doesn't have what they call a "reader" in CL or other traditional LISPs. Strings with lisp-expressions in newLISP get compiled first to an internal format then this gets evaluated. There is also an 'eval' in newLISP but it works only on the internal format not on strings directly:



(set 'x '(+ 3 4)) => (+ 3 4)



x => (+ 3 4)



(eval x) => 7



(eval-string "(+ 3 4)") => 7



Also, thanks to Bat for catching the doc error for 'random'



Lutz

HPW

#3
For alisp compatibility I use this:
(define (read readstr    readret)
(cond
((float readstr)
(if (find "." readstr)
(setq readret (float readstr))
(setq readret (integer readstr))
)
)
((=(slice readstr 0 1)"(")
(setq readret(eval-string(append "'" readstr)))
)
(true
(setq readret (symbol readstr))
)
)
)
Hans-Peter