lisp - 让方案出错

标签 lisp scheme

我在方案中编写了以下代码片段,

(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 教程并尝试编写一些基本程序以掌握它,特别是 herelet 特殊形式的文档和正确结构:

(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/

相关文章:

list - 方案 - 列表的子集

scheme - Racket /Lisp 错误 : expected a procedure that can be applied to arguments

javascript - 在 JavaScript 中实现 Lisp 的缺点

lisp - 带有蹦床和 Y 组合器的代码应该在具有动态作用域的 lisp 中工作吗?

functional-programming - 读SICP的前置条件是什么?

lisp - 如何在Scheme/Lisp 中对符号进行算术计算?

scheme - 需要解释 Lisp 中的反向科学记数法函数

functional-programming - 计划作业

scheme - 在Scheme中将字符串转换为整数

scheme - 到达定义结构中的特定项目