Page 1 of 1

How about (read) ?

Posted: Fri Oct 10, 2003 3:52 pm
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.

Posted: Fri Oct 10, 2003 4:49 pm
by eddier
To read a whole file into one big string:

(read-file "filename")

Eddie

Posted: Fri Oct 10, 2003 11:13 pm
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'

Lutz

Posted: Thu Nov 27, 2003 1:38 pm
by HPW
For alisp compatibility I use this:

Code: Select all

(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))
		)
	)
	)