Introductory Lisp Program for Reading a File of Numbers
Into a One Dimensional Array
;;LOAD TESTFILE.TXT here.
(defparameter *Count* 0)
(defun file-read (filename)
(let ((myArray) ;;local variables
(line)
)
(with-open-file (infile filename :direction :input)
(read-line infile) ;;Skip over the first two lines
(read-line infile)
(setf line (read-line infile))
(setf *Count* (read infile))
(format t "~a~%Count:~D~%" line *Count*)
(setf myArray (make-array (list *Count*)))
(dotimes (row *Count*)
(setf (aref myArray row) (read infile))
)
)
myArray ;;Return the array
)
)
(defun file-write (file arrayname)
(with-open-file (outfile file :direction :output
:if-exists :supersede)
(format outfile "Here's the file we just read:~%")
(dotimes (row *Count*)
(format outfile "~6D" (aref arrayname row))
(if (= (rem row 5) 4) (terpri outfile))
)
(terpri outfile)
(format outfile "And each value squared:~%")
(dotimes (row *Count*)
(format outfile "~10D" (sqr (aref arrayname row)))
)
)
)
(defun sqr (x)
"Returns X squared."
(* x x)
)
(defun test ()
(let ((array)
)
(setf array (file-read "testfile.txt"))
(print array) ;;Prints out the array in Lisp format
(file-write "newTestFile.txt" array)
)
)
(test) ;;This runs the program when you load it