How about (read) ?

Q&A's, tips, howto's
Post Reply
Posts: 10
Joined: Fri Oct 10, 2003 3:38 pm

How about (read) ?

Post by 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.

Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

To read a whole file into one big string:

(read-file "filename")


Posts: 5282
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Post by Lutz »

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'


Posts: 1377
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany

Post by HPW »

For alisp compatibility I use this:

Code: Select all

(define (read readstr    readret)
		((float readstr)
		 (if (find "." readstr)
			(setq readret (float readstr))
			(setq readret (integer readstr))
		((=(slice readstr 0 1)"(")
			(setq readret(eval-string(append "'" readstr)))
			(setq readret (symbol readstr))

Post Reply