READ-SEQUENCE 从文件中读取字符。当您计算file-length 并调用read-sequence 时,您所做的就是读取平面列表中的所有字符。即,您的示例中的lst 是这个列表:
(#\( #\1 #\ #\2 #\ #\3 #\ #\( #\4 #\ #\5 #\ #\( #\1 #\2 #\ #\1 #\1 #\
#\9 #\ #\6 #\) #\ #\4 #\ #\8 #\ #\( #\7 #\7 #\ #\5 #\3 #\( #\4 #\7 #\)
#\) #\ #\( #\1 #\2 #\ #\1 #\5 #\ #\1 #\8 #\) #\) #\Newline)
您可以看到此列表中的所有元素都是字符,它们用#\... 语法表示。例如,第一项描述如下(使用 SBCL 测试,实际输出可能在您的实现中有所不同):
* (describe (first lst))
#\(
[standard-char]
Char-code: 40
Char-name: LEFT_PARENTHESIS
; No value
(with-open-file (stream "/tmp/file-list.txt")
(读 (make-concatenated-stream (make-string-input-stream "(")
溪流
(make-string-input-stream ")"))))
您要做的是在该文件上调用READ:
* (with-open-file (in "/tmp/file-list.txt")
(read in))
; Evaluation aborted on #<END-OF-FILE {1013420B23}>.
而且您的输入文件似乎也缺少右括号。修复后,您有:
* (with-open-file (in "/tmp/file-list.txt")
(read in))
(1 2 3 (4 5 (12 11 9 6) 4 8 (77 53 (47)) (12 15 18)))
这里读取的值是一个数字列表和嵌套列表。
* (describe (first *))
1
[fixnum]
; No value
---- 编辑
您的flatten-list 函数似乎有效,我的意思是您的输入列表在另一个文件中,您需要通过调用read 使用标准Lisp 阅读器提取数据:
* (with-open-file (in "/tmp/file-list.txt")
(flatten-list (read in)))
(1 2 3 4 5 12 11 9 6 4 8 77 53 47 12 15 18)
--- 编辑 2
如果您的文件包含列表元素,如下所示:
1 2 3 (4 5 (12 11 9 6) 4 8 (77 53(47)) (12 15 18))
然后你可以写一个循环,如下:
(loop
for form = (read in nil in)
until (eq form in)
collect form)
或者,您可以使用串联流:
USER> (with-input-from-string (open "(")
(with-input-from-string (close ")")
(with-open-file (file "/tmp/file-list.txt")
(read (make-concatenated-stream open file close)))))
(1 2 3 (4 5 (12 11 9 6) 4 8 (77 53 (47)) (12 15 18)))
或等效:
(with-open-file (stream "/tmp/file-list.txt")
(read (make-concatenated-stream (make-string-input-stream "(")
stream
(make-string-input-stream ")"))))