小编Goo*_*ons的帖子

Common Lisp从文件读取并存储为列表

所以做一些常见的lisp练习,一切都很顺利,直到我遇到这种奇怪的行为.我从文件(brown.txt)读取文本到可变语料库,它应该存储为列表.然而,我怀疑它不是,尽管它有时像一个一样,但在其他时候失败.

以下是文件的基本读取 - >追加列表 - > 语料库中的商店列表(在空白处拆分/标记化):

(defun tokenize (string)
  (loop
     for start = 0 then (+ space 1)
     for space = (position #\space string :start start)
     for token = (subseq string start space)
     unless (string= token "") collect token
     until (not space)))

(defparameter *corpus*
   (with-open-file (stream "./brown.txt" :direction :input)
     (loop for line = (read-line stream nil)
           while line
           append (tokenize line))))
Run Code Online (Sandbox Code Playgroud)

下面是两个表达式,它们都应该起作用,但只有后者才起作用(语料库一个).第一个返回NIL.

(loop for token in *corpus* do
     (print token))
*corpus*
Run Code Online (Sandbox Code Playgroud)

我怀疑它与从文件作为 …

sbcl common-lisp

3
推荐指数
1
解决办法
732
查看次数

标签 统计

common-lisp ×1

sbcl ×1