函数返回列表,但在LISP中打印出NIL

1 lisp clisp common-lisp file-read

我正在按字符读取文件char,并构造一个由单词字母列表组成的列表。我是这样做的,但是在测试时会打印出NIL。当我打印出列表时,也在测试功能之外,它打印效果很好。这里有什么问题?LET关键字还有其他含义吗?

这是我的阅读功能:

(defun read-and-parse (filename)
  (with-open-file (s filename)
    (let (words)
      (let (letter)
        (loop for c = (read-char s nil)
              while c
              do(when (char/= c #\Space)
                  (if (char/= c #\Newline) (push c letter)))
              do(when (or (char= c #\Space) (char= c #\Newline) )
                  (push (reverse letter) words)
                  (setf letter '())))
        (reverse words)
))))
Run Code Online (Sandbox Code Playgroud)

这是测试功能:

(defun test_on_test_data ()

    (let (doc (read-and-parse "document2.txt"))
        (print doc)
))
Run Code Online (Sandbox Code Playgroud)

这是输入文本:

hello
this is a test
Run Code Online (Sandbox Code Playgroud)

Bar*_*mar 5

您使用不let正确。语法为:

(let ((var1 val1)
      (var2 val2)
      ...)
  body)
Run Code Online (Sandbox Code Playgroud)

如果变量的初始值为,则NIL可以缩写(varN nil)为just varN

你写了:

(let (doc 
      (read-and-parse "document2.txt"))
  (print doc))
Run Code Online (Sandbox Code Playgroud)

基于上面的内容,这是缩写形式,它等效于:

(let ((doc nil)
      (read-and-parse "document2.txt"))
  (print doc))
Run Code Online (Sandbox Code Playgroud)

现在您可以看到它绑定docNIL,并将变量绑定read-and-parse"document2.txt"。它从不调用该函数。正确的语法是:

(let ((doc (read-and-parse "document2.txt")))
  (print doc))
Run Code Online (Sandbox Code Playgroud)