Reputation: 33
I have a .txt file like this:
0
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
7
8
And i want LISP to read the text file and generate two lists, the first one with the first nine values of the text file and the second one with the next nine values. Like these: List1 = (0 1 2 3 4 5 6 7 8), List2 = (0 1 2 3 4 5 6 7 8). I have these code:
(DEFUN text()
(SETQ l NIL)
(LOOP
(UNLESS (NULL l) (SETQ LST1 (LIST (READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l))
LST2 (LIST (READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l)(READ l))
eb (LST1) em (LST2))
(CLOSE l) (RETURN (ASTAR LST1 LST2)))
(SETQ l (OPEN filename :if-does-not-exist nil))))
Upvotes: 2
Views: 1908
Reputation: 38799
let
. Global variables make your code fragile w.r.t. side-effects and clutter the global namespace.with-open-file
.(defun read-18-lines-of-integers-in-two-lists (file)
(with-open-file (in file)
(loop repeat 2
collect (loop repeat 9
collect (parse-integer (read-line in))))))
(read-18-lines-of-integers-in-two-lists #P"/tmp/test")
=> ((0 1 2 3 4 5 6 7 8) (0 1 2 3 4 5 6 7 8))
Upvotes: 10