Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is read-line run twice for reading from a file in Lisp?

This is the code to implement the 'cat' command with lisp, as is explained in the book ANSI Common Lisp, page 122.

(defun pseudo-cat (file)
  (with-open-file (str file :direction :input)
    (do ((line (read-line str nil 'eof)
               (read-line str nil 'eof)))
        ((eql line 'eof))
      (format t "~A~%" line))))

Why is the read-line function run twice? I tried to run it with only one read-line, but the Lisp couldn't finish the code.

like image 480
prosseek Avatar asked Sep 25 '10 23:09

prosseek


1 Answers

The syntax of DO variables is: variable, initialization form, update form. In this case, the initialization form is the same as the update form. But there is no shorthand for that case in DO, so you have to write it out twice.

like image 66
Xach Avatar answered Oct 13 '22 00:10

Xach