Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types LISP:该值不是预期的列表类型_Types_Lisp_Common Lisp - Fatal编程技术网

Types LISP:该值不是预期的列表类型

Types LISP:该值不是预期的列表类型,types,lisp,common-lisp,Types,Lisp,Common Lisp,我对用LISP编写代码非常陌生,我正在尝试编写这个初步的代码片段,将文件中的单词读入一个参数,然后打印出该列表中的所有单词,以确保所有单词都被放入其中。这就是我到目前为止所做的: (defparameter *wordlist* nil) (defun run() (get-words-from-file) (print-wordlist *wordlist*)) (defun get-words-from-file () (let ((in (open "/Users/levi

我对用LISP编写代码非常陌生,我正在尝试编写这个初步的代码片段,将文件中的单词读入一个参数,然后打印出该列表中的所有单词,以确保所有单词都被放入其中。这就是我到目前为止所做的:

(defparameter *wordlist* nil)

(defun run()
  (get-words-from-file)
  (print-wordlist *wordlist*))

(defun get-words-from-file ()
  (let ((in (open "/Users/levibanks/Desktop/cs352/program3/wordlist.txt")))
    (dotimes (n 500)
      (setq *wordlist* (append (read-line in))))
  (close in)))

(defun print-wordlist (wordlist)
  (when wordlist
    (print (car wordlist))
    (print-wordlist (cdr wordlist))))
但是,当我尝试运行这段代码时,它给出了一个错误“值‘brown’[我正在读取的文件中的一个单词]不是预期的类型列表。”

我真的不知道为什么这不应该工作,因为这是我以前看到的打印列表,所以任何帮助都将非常感谢

你应该仔细阅读

特别是,
(setq a(append b))
不会向
a
的上一个值追加任何内容

你需要的是

(defun read-lines-from-file (file-name)
  (with-open-file (input file-name)
    (loop for line = (read-line input nil nil)
       while line collect line)))

(defparameter *wordlist* (read-lines-from-file "/Users/levibanks/Desktop/cs352/program3/wordlist.txt"((
你应该仔细阅读

特别是,
(setq a(append b))
不会向
a
的上一个值追加任何内容

你需要的是

(defun read-lines-from-file (file-name)
  (with-open-file (input file-name)
    (loop for line = (read-line input nil nil)
       while line collect line)))

(defparameter *wordlist* (read-lines-from-file "/Users/levibanks/Desktop/cs352/program3/wordlist.txt"((

谢谢你的帮助!在我发布这篇文章之后,我意识到我没有正确地使用append,因为(A)我从文件中读取的单词没有被视为列表,(B)我没有给append两个参数(即wordlist和读取行)。
(defun get words from file()(let((in(open)/Users/levibanks/Desktop/cs352/program3/wordlist.txt)))(dotimes(n 500)(setq*wordlist*(append*wordlist*(list(read line in‘‘‘‘‘‘‘))(close in))
感谢您的帮助!我在发布本文后意识到,我没有正确使用append,因为(A)我从文件中读取的单词不被视为列表,并且(B)我没有给append两个参数(即单词列表和读取行)。
(从文件()中获取单词)(let((in(open)/Users/levibanks/Desktop/cs352/program3/wordlist.txt))(dotimes(n500)(setq*wordlist*(append*wordlist*(list(read line in‘‘)'))(close in))