【发布时间】:2011-01-07 12:54:53
【问题描述】:
我正在尝试在 Lisp 中编写一个简单的过程来将元素插入到二叉搜索树中。
我将树表示为一个列表:
- 树中的第一个元素是根
- 第二个元素是左子树
- 第三个元素是右子树
这是我的代码:
(define Insert
(lambda (x T)
(if (null? T)
(list x '() '())
(if (> x (car T))
(list (car T)
(cadr T)
(Insert x (list (caddr T))))
(list (car T)
(Insert x (cadr T))
(list (caddr T)))))))
当我像这样调用过程:(Insert 2 '(4 '(2 '() '() ) '())) 时,出现 ">" 的问题,因为第二个参数不是实数,但我不知道为什么。
例外:
>: expects type <real number> as 2nd argument, given: quote; other arguments were: 2
但是,当我这样调用程序时:(Insert 2 ( list 4 (list 2 '() '() ) '())),
成功了。
为什么?
我知道'(1 '() '()) 和(list 1 '() '()) 是相等的,不是吗?
【问题讨论】:
标签: list functional-programming lisp scheme