(defun read-file-list (infile)
(with-open-file (instream infile :direction :input :if-does-not-exist nil)
(when instream
(let ((list (make-list (file-length instream))))
(read-sequence list instream)
list))))
(setq lst (read-file-list "/home/Desktop/nested_list.txt"))
(flatten-list lst )
(print lst)
;(1 2 3 (4 5 (12 11 9 6) 4 8 (77 53(47)) (12 15 18))
; file has got this line
Clisp reading list from file and flatten it, but not working, doesnt recognize lst as a list
152 Views Asked by AudioBubble At
1
READ-SEQUENCE
reads characters from a file. When you compute thefile-length
and callread-sequence
, all you are doing is reading all characters in a flat list. Namely,lst
in your example is this list:You can see that all elements in this list are characters, they are denoted with the
#\...
syntax. For example, the first item is described as follow (tested with SBCL, actual output may vary in your implementation):(with-open-file (stream "/tmp/file-list.txt") (read (make-concatenated-stream (make-string-input-stream "(") stream (make-string-input-stream ")")))) What you want to do is to call
READ
on that file:And it appears also your input file misses a closing parenthesis. After fixing that, you have:
Here the read value is a list of numbers and nested lists.
---- Edit
Your
flatten-list
function seems to work, what I am saying is that your input list is in another file, and that you need to extract the data using the standard Lisp reader by callingread
:--- Edit 2
If your file contains the list elements, like this:
Then you could write a loop, as follows:
Or, you could use a concatenated stream:
Or equivalently: