I'm struggling to open a file, and read each line until EOF. I'm using nyquist, which is based on XLISP, and lacks Common Lisp's with-open-file. My code is:
(let ((in_file (open "/home/soodesune/testfile.csv" :if-does-not-exist nil)))
(loop for line = (read-line in_file)
while line do (print line))
(close in_file))
the output im getting from nyquist is:
error: unbound variable - LINE
I'd love some help understanding where I'm going wrong
Note: I also tried:
(loop for line = (read-line in_file nil)
But that gives:
error: too many arguments
UPDATE:
Using the information and link that Rainer Joswig provided I was able to find typical code one might use to open a file and read it line by line:
1 (do* ((fp (open "test.dat" :direction :input))
2 (ex (read fp nil) (read fp nil)))
3 ((null ex) (close fp) nil)
4 (print ex))
The do function has three basic parts:
- lines 1 and 2 - variables assigned for use in the loop, their initial values and how to increment them. In this case
fpandex.fpis not incremented, andexis initialized and incremented by onereadfrom from fp (I have no idea what that trailingnilis for) - line 3 - The exit condition for the loop, and a list of
return expressionsall of which are evaluated on exit, and the last of which is returned. In this case exit will occur whenex == nulland thennilwill be returned - line 4 - The actual body of the loop. Self explanatory in this case.
note: do* differs from do in that it allows variables assigned in one expression to be used in a later one, like how fp is used in the assignment of ex
I'm a total n00b at any kind of LISP, so if any of the above is wrong, please speak up.
Nyquist is not based on Common Lisp. It is based on an early XLISP. It only supports a simple LOOP and not the complex LOOP of Common Lisp.
See the Nyquist manual:
http://www.cs.cmu.edu/~rbd/doc/nyquist/part19.html#226
Its LOOP syntax is:
It does not support the extended LOOP syntax of Common Lisp.