我正在尝试制定一个程序,该程序将一个数字与一组从最小到最大排序的数字邻接。在此集合中,没有重复项,因此我尝试通过检查要连接的数字是否已经等于集合的car
并通过返回该集合(如果返回true)来递归地解决此问题,并且如果数字大于集合的car
,则if将再次使用数字和集合的cdr
执行该过程。如果集合为null,则它将与该数字邻接,因为我们知道该数字不等于集合中的任何其他数字。
这是我写的代码:
(define (adjoin-set x set)
(if (null? set)
(cons x set)
(let (x1 (car set))
(cond ((= x x1) set)
((> x x1)(adjoin-set x (cdr set)))
(else (cons x set))))))
我通过一组几率对其进行了测试:(define odds '(1 3 5)) (adjoin-set 7 odds)
和the interpreter返回了以下内容:Error: let: need a pair for bindings: got x1 []
。谁能解释为什么退回该邮件以及如何解决?
答案 0 :(得分:1)
您的let
表达式的语法不正确,缺少几个()
。应该是这样的:
(define (adjoin-set x set)
(if (null? set)
(cons x set)
(let ((x1 (car set)))
(cond ((= x x1) set)
((> x x1) (adjoin-set x (cdr set)))
(else (cons x set))))))
您可以在let
声明中定义多个变量/值对,最外面的括号用于将它们全部分组在一起。