syntax-error - SBCL : what's wrong with this let? 生物医学信息学原理

标签 syntax-error lisp common-lisp let

这里是 Lisp 新手,倾向于相信我肯定误解了一些东西,因为这是第二版相当长的书的第 35 页的问题。我一直在阅读关于 let 绑定(bind)的文章,所以让我通过引用他来确保作者的*意图是明确的:

In the following code, the symbol pos serves as a temporary storage or variable that has as its value the result of the position function expression. Finally, how will we accumulate the results? The recursive call should give us a list of the remaining positions, so the first one found should just be put on the front. We already have a function to do this, the cons function

这是代码:

(defun all-pos (item seq start)
  (let ((pos (position item seq :start start))
    (if pos
        (cons pos
          (all-pos item seq (+ 1 pos)))
    nil))))

这是错误:

Ch1_Notes.lisp:27:5:
  error: 
    The LET binding spec (IF POS
                             (CONS POS (ALL-POS ITEM SEQ (+ 1 POS)))
                             NIL) is malformed.

Compilation failed.

这是不言而喻的错误吗?我还应该包括一些前面的代码吗?

*作者 Ira J. Kalet 已经去世,所以我不能问他。

最佳答案

如果出现这样的错误,重新缩进代码通常很有用。通常可以在编辑器中使用键盘命令来完成。

但是 Lisp 还带有内置代码格式,称为 pretty-print :

CL-USER > (let ((*print-right-margin* 60))
 (pprint '

; your code follows:

(defun all-pos (item seq start)
  (let ((pos (position item seq :start start))
    (if pos
        (cons pos
          (all-pos item seq (+ 1 pos)))
    nil))))

))

输出看起来像这样:

(DEFUN ALL-POS (ITEM SEQ START)
  (LET ((POS (POSITION ITEM SEQ :START START))
        (IF POS (CONS POS (ALL-POS ITEM SEQ (+ 1 POS)))
         NIL))))

这样可以更容易地看出 ifpos 变量绑定(bind)处于同一缩进级别。 这不可能是对的!

要使用 Lisp 格式化您的代码,您只需要:

(let ((*print-right-margin* 60))
  (pprint '

; here goes your code

))

Lisp 会为你格式化...

关于syntax-error - SBCL : what's wrong with this let? 生物医学信息学原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712101/

相关文章:

loops - 为迭代定义一个宏

emacs - 在 emacs 中更新字体锁定关键字而无需重新加载主要模式

php - Laravel 5 View语法错误

reactjs - 如何修复 reactjs 语法错误 : unexpected token?

emacs - 在 Mac 上安装和使用 LISP

function - Common Lisp - 从数字列表构建大于、小于或等于给定数字的列表的函数

sql - 是什么原因导致 “Syntax error in INSERT INTO statement”

java - 修饰符组合非法 : public and private

scala - LISP 的代码即数据意识形态与高阶函数基本相同吗?

macros - 普通口齿不清 : Best method to temporarily import a few functions from a package