Lisp中列表的问题 [英] Problem with list in Lisp

查看:139
本文介绍了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'()'( ))'())),我遇到了>的问题,因为第二个参数不是实数,但我不知道为什么。 b
$ b

例外:

 >:期望输入< real number>作为第二个参数,给出:quote;其他的参数是:2 

然而,当我像这样调用过程时: (插入2(列表4(列表2'()'())'()))
它可以成功地运行。

<为什么?



我知道'(1'()'())(list 1'()'())是等于的,不是吗?

解决方案

'(1'())相当于(list 1(list'quote nil))。我怀疑如果你删除内部引号字符,你会没事的。

因此,生成表达式的带引号的表达式等于(list 1'()'()) is '(1()())


I am trying to write a simple procedure in Lisp to insert an element into binary search tree.

I represented the tree as a list:

  • the first element in the tree is the root
  • the second element is the left sub-tree
  • the third element is the right sub-tree

This is my code:

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

When I call the procedure like this: (Insert 2 '(4 '(2 '() '() ) '())), I get a problem with ">" because the second argument isn't a real number, but I don't know why.

The exception:

>: expects type <real number> as 2nd argument, given: quote; other arguments were: 2

However, when I call the procedure like this: (Insert 2 ( list 4 (list 2 '() '() ) '())), it works successfully.

Why?

I know that '(1 '() '()) and (list 1 '() '()) are equals, aren't they?

解决方案

'(1 '()) is equivalent to (list 1 (list 'quote nil)). I suspect that if you drop the "internal" quote characters, you will be fine.

So, the quoted expression that generates an expression that is equal to (list 1 '() '()) is '(1 () ()).

这篇关于Lisp中列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆