lispaudacitynyquist

LISP/Nyquist opening a file and reading line-by-line


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:

  1. lines 1 and 2 - variables assigned for use in the loop, their initial values and how to increment them. In this case fp and ex. fp is not incremented, and ex is initialized and incremented by one read from from fp (I have no idea what that trailing nil is for)
  2. line 3 - The exit condition for the loop, and a list of return expressions all of which are evaluated on exit, and the last of which is returned. In this case exit will occur when ex == null and then nil will be returned
  3. 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.


Solution

  • 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:

    (loop expr...)
    

    It does not support the extended LOOP syntax of Common Lisp.