简体   繁体   English

缺点论证的不同规则?

[英]Different rules for cons arguments?

I'm new in Racket language and I bumped into an issue. 我是球拍语言的新手,遇到了一个问题。 I'm now trying to implement Binary Search Tree using cons(list). 我现在正在尝试使用cons(list)实现Binary Search Tree。

This is a simple BST I tried to make: 这是我尝试制作的简单BST:

在此处输入图片说明

For this BST, if I convert this into Racket list, this can be one possibility: '(6, (4, (), ()), (7, (), ()) 对于此BST,如果将其转换为球拍列表,则可能是一种可能性:'(6,(4,(),()),(7,(),())

To produce this format of list, I used this code: 为了产生这种列表格式,我使用了以下代码:

(define x3 (cons 6 (cons (cons 4 (cons null (cons null null)))
                         (cons 7 (cons null (cons null null)))

And for this code, the result is below: 对于此代码,结果如下:

'(6 (4 () ()) 7 () ())

As you see, this is not what I wanted. 如您所见,这不是我想要的。 The second child node 7 () () is not inside the bracket, which means it doesn't exist as an list. 第二个子节点7()()不在括号内,这意味着它不作为列表存在。 It acts as individual 3 elements, not single list. 它充当单独的3个元素,而不是单个列表。 So I changed a bit: 所以我改变了一点:

(define x2 (cons 6 (cons (cons (cons 4 (cons null (cons null null))) null)
                         (cons (cons 7 (cons null (cons null null))) null)
                   )
           )
)

And for this 2nd code, the result is as following: 对于第二个代码,结果如下:

'(6 ((4 () ())) (7 () ()))

And this is not what I wanted either. 这也不是我想要的。 The first child node ((4 () ())) now is inside and inside the list. 现在,第一个子节点((4()()))在列表的内部和内部。 So I did the final try, and the code is like this: 所以我做了最后的尝试,代码是这样的:

(define x3 (cons 6 (cons (cons 4 (cons null (cons null null)))
                         (cons (cons 7 (cons null (cons null null))) null)
                   )
           )
)

And the result looks like this: 结果看起来像这样:

'(6 (4 () ()) (7 () ()))

Now it works! 现在可以了! The question is, why these things happen? 问题是,为什么这些事情会发生? I kind of realized that the rule is different between the last element of list and the other when cons is used, but after all, aren't they same kind of list? 我有点意识到使用cons时list的最后一个元素与另一个元素之间的规则是不同的,但是毕竟,它们不是同一类型的list吗?

When writing deep list-of-lists using cons , it can be very easy to lose track of where an inner list begins, or where it ends, etc. So if you are writing out a long list by hand, it might be easier to simply use list : 当使用cons编写深列表列表时,很容易忘记内部列表的开始位置或结束位置等。因此,如果您要手工写一个长列表,则可能更容易只需使用list

(list 6 (list 4 null null) (list 7 null null))

Otherwise, you can also build your cons list easily by writing it from the inside out. 否则,您也可以通过从内到外的方式轻松构建自己的cons列表。 For example, in your BST, there are two leaf nodes with one root parent node. 例如,在您的BST中,有两个叶节点和一个根父节点。 So you can start from the leaf nodes: 因此,您可以从叶节点开始:

(define left   (cons 4 (cons null (cons null null))))
(define right  (cons 7 (cons null (cons null null))))

then the BST becomes: 那么BST变为:

(cons 6
      (cons left
            (cons right null)))

which is equivalent to: 等效于:

(cons 6
      (cons (cons 4 (cons null (cons null null)))
            (cons (cons 7 (cons null (cons null null))) null)))

First of all: 首先:

cons creates actually a cell, a cons cell. cons实际上创建了一个单元格,即一个cons单元格。 You can append using cons several cons cells to each other. 您可以使用cons将多个cons单元相互附加。

(cons 'a 'b) ;; (a . b)
(cons 'c (cons 'a 'b)) ;; (c . (a . b)) 

The lisp notation says, ' . Lisp表示法为'。 ( ' is ' ', so the last example simplifies to: ('为'',因此最后一个示例简化为:

(c a . b)

What is a list? 什么是清单? Such cons cells or chain of cons cells with last element being ' . 这样的缺点细胞或缺点细胞链,最后一个元素为'。 ()'. ()”。 In this case, you are allowed to make it invisible. 在这种情况下,您可以使其不可见。

(cons 'c (cons 'a null)) ;; or 
(cons 'c (cons 'a 'null)) ;; or
(cons 'c (cons 'a '()))   ;; which are the same!
;; in common lisp even (cons 'c (cons 'a ())) in addition

Is thus simplified to: 因此简化为:

(c a) ;; actually (c . (a . null))

These syntax simplification rules and the fact that cons takes only exact 2 arguments but you need 3 slots for the binary tree, makes the cons-constructs more complicated than the problem looks at the first sight. 这些语法简化规则以及cons仅使用精确的2个参数,但二进制树需要3个插槽的事实,使得cons构造比问题乍一看更复杂。

Your BST should be expressed as lists: 您的BST应该表示为以下列表:

(list 6 (list 4 null null) (list 7 null null))

which is already very complicated to express only with conses manually ... (as explained by assefamaru) 仅手动使用conses就已经非常复杂...(如assefamaru所述)

As you see, list is an abstraction over the nesting of conses with the innermost cons - cell consing to a null, and more adapted to the simplication rules of the syntax. 如您所见, list是cons嵌套的最抽象,cons嵌套具有最里面的cons-单元cons为null,并且更适合语法的简化规则。 Thus much more brain-friendly. 因此对大脑更友好。

But more elegantly, you should use structs (as explained here https://learningtogetolder.wordpress.com/2013/08/14/creating-a-binary-search-tree-in-racket/ ) by defining that each node expects a value and a left and a right node. 但更优雅的是,您应该通过定义每个节点都希望使用结构来使用结构(如此处https://learningtogetolder.wordpress.com/2013/08/14/creating-a-binary-search-tree-in-racket/所述 )值以及左节点和右节点。 Thereby, construction errors are made impossible. 因此,使构造错误成为不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM