我在方案中编写了以下代码片段,
(define (test-for-prime number divisor)
(cond (prime? number) (number)
(else (let ((next-divisor) (find-next-divisor number (+ 1 divisor)))
(test-for-prime (/ number next-divisor) (next-divisor))))))
但是,我得到以下错误
let: bad syntax (not an identifier and expression for a binding) in: (next-divisor)
如何纠正?
最佳答案
试试这个版本,它修复了所有语法错误:
(define (test-for-prime number divisor)
(cond ((prime? number)
number)
(else
(let ([next-divisor (find-next-divisor number (+ 1 divisor))])
(test-for-prime (/ number next-divisor) next-divisor)))))
你有很多错位的括号。有些遗漏了,有些是错误的...我建议您仔细阅读 Scheme 教程并尝试编写一些基本程序以掌握它,特别是 here是 let
特殊形式的文档和正确结构:
(let ([id val-expr] ...) body ...+)
The first form evaluates the val-exprs left-to-right, creates a new location for each id, and places the values into the locations. It then evaluates the bodys, in which the ids are bound. The last body expression is in tail position with respect to the let form. The ids must be distinct according to bound-identifier=?
此外,使用 IDE/编辑器也是一个好主意,它可以突出显示此类问题并帮助您正确缩进代码。特别要注意的是,当你这样写时,变量不能被括在括号中: (x)
Scheme 假定 x
是一个过程并且你正在调用
关于lisp - 让方案出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14003270/