这里是 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 theposition
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, thecons
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))))
这样可以更容易地看出 if
与 pos
变量绑定(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/