Reputation: 422
I have lets say the following list in a .txt file
(5 3 1)
All i am trying with following source code is to store the above list in a variable in LISP. Until the first format everything seems right. But then i realize that *originalStateVar*
is not getting treated as a list with 3 atoms but as a list with 1 atom. Source code follows:
(defvar *originalStateVar*)
(defun fileInput ()
(let ((i 1)(in (open *originalStateLocation* :if-does-not-exist nil)))
(when in
(loop
for line = (read-line in nil)
while line do
(format t "~a~%" line) ;debug line
(format t "i is <~a>~%" i) ;debug line
(setf *originalStateVar* (list line)) ;storing list in variable
(setf i (+ i 1))) ;debug line
(close in))
(format t "originalStateVar is <~a>" (car *originalStateVar*))
(format t "second element originalStateVar is <~a>~%" (cadr *originalStateVar*))
(format t "third element originalStateVar is <~a>~%" (caddr *originalStateVar*))))
The output of the above code though is:
(5 3 1)
i is <1>
first element originalStateVar is <(5 3 1)>
second element originalStateVar is <NIL>
third element originalStateVar is <NIL>
All i can tell is that it stores the (5 3 1) as a single atom in a list, so it becomes something like ((5 3 1))
and that's why cadr
returns NIL
.
Any help on how to make *originalStateVar*
get treated that way, would be greatly appreciated!
Upvotes: 0
Views: 373
Reputation: 139251
WITH-OPEN-FILE
instead of OPEN
READ
instead of READ-LINE
Example:
CL-USER 11 > (let ((*read-eval* nil))
(with-open-file (in "/tmp/test.data")
(read in)))
(1 2 3)
CL-USER 12 > (let ((*read-eval* nil))
(with-open-file (in "/tmp/test.data")
(describe (read in))))
(1 2 3) is a LIST
0 1
1 2
2 3
Upvotes: 5