0
;;; <- can one use cons to do  ((a . b) . (c . d))?


(define x (cons a b)); nil -- should it be error
(define x (cons 'a 'b)); (a . b)

(define y (cons 'c 'd)); (c . d)

(define z00 (cons x y)) ; (((a . b) c . d) <- cannot use cons to do  ((a . b) . (c . d))?
(define z01 (cons x 'y)) ; ((a . b) . y) 
(define z10 (cons 'x y)) ; (x c . d) 
(define z11 (cons 'x 'y)); (x . y))

(define z (list x y z00 z01 z10 z11)) 
        ; ((a . b) (c . d) ((a . b) c . d) ((a . b) . y) (x c . d) (x . y))

;;; 如果没有任何其他方式或点对不能有这样的第二个元素?

4

3 回答 3

2

是的你可以。该语言有一个很棒的谓词叫做equal?它可以让你测试这个:

> (equal? (cons (cons 'a 'b) (cons 'c 'd))
          '((a . b) . (c . d)))
#t
> (equal? '((a . b) . (c . d))
          '((a . b) c . d))
#t

你甚至可以编写一个小显示函数来确认这一点:


(define (display-thing thing)
  (if (cons? thing)
      (begin
        (display "(")
        (display-thing (car thing))
        (display " . ")
        (display-thing (cdr thing))
        (display ")"))
      (display thing)))

现在

> (display-thing (cons (cons 'a 'b) (cons 'c 'd)))
((a . b) . (c . d))
> (display-thing '((a . b) . (c . d)))
((a . b) . (c . d))
> (display-thing '((a . b) c . d))
((a . b) . (c . d))

这一切都应该告诉您的是,这((a . b) . (c . d))只是((a . b) c . d)编写结构相同对象的不同方式

于 2021-04-18T10:23:27.980 回答
1

对根据其内容进行不同的可视化。如果cdr一对中的 包含空列表,则它是一个正确的列表,并且不显示点和额外的空列表:

(cons 'a '())
'(a . ())
; ==> (a)

具有 pair 的 paircdr可以被可视化为一个列表元素,没有.额外的括号:

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

这些只是为了让我们可以(1 2 3)展示而不是(1 . (2 . (3 . ())))它的真实制作方式。

如果您没有一对或空列表,cdr那么它会退回到显示点对:

(cons 'a 'b)
'(a . b)
; ==> (a . b)

在您的示例'((a . b) . (c . d))中,因为在点之后有一对(例如,cdr如果可视化将删除点和一对括号并将其显示为((a . b) c . d). 这是 REPL 显示它的唯一可接受的正确方法,即使两者您的示例和显示将作为相同的结构读入。

数字也有类似的问题。在代码中,您可以使用10,#xa#o12获取数字 10,并且该值将不知道读取的格式是什么,并且仅在 REPL 中显示以 10 为底的值。

于 2021-04-19T00:38:44.543 回答
-1

;;; ```
;;; <- can one use cons to do  ((a . b) . (c . d))?


(define x (cons a b)); nil -- should it be error
(define x (cons 'a 'b)); (a . b)

(define y (cons 'c 'd)); (c . d)

(define z00 (cons x y)) ; (((a . b) c . d) <- cannot use cons to do  ((a . b) . (c . d))?
(define z01 (cons x 'y)) ; ((a . b) . y) 
(define z10 (cons 'x y)) ; (x c . d) 
(define z11 (cons 'x 'y)); (x . y))
(define z22 (cons '(f g) '(h i)))
(define z2c (cons (cons 'f 'g) (cons 'h 'i)))

(define fgc (cons ('f 'g))); should it be error (nil) 
; actually not as 'f is an exoression (quote f) and f for some reason is nil it becomes nil from quote of nil abd so is the second one.  now (nil . nil) is (nil)

(define z (list x y z00 z01 z10 z11 z22 z2c fgc)) 
        ; ((a . b) (c . d) ((a . b) c . d) ((a . b) . y) (x c . d) (x . y))



;;; ```

;;; 如果没有任何其他方式或点对不能有这样的第二个元素?

;;; 可能不是


;;; as the cons join 2 pairs of dotted pairs and can generate one dotted pair
;;; ((a . b) . (c . d)) but the printing rule is reflected the list bias 

;;; this new dotted pair will have the first element as (( a . b) ... print as
;;;   ((a . b) ...
;;; the 2nd element it will consider whether it is an atom or another dotted pair 
;;;      (other possibilities like loop back or something else ... not sure)
;;; as (c . d) is a dotted pair the "printing" continues as a list would
;;; 
;;;      ((a . b) c ... 

;;; however the second element of (c . d) is not a dotted pair but an atom and print as 

;;;               . d) will it becomes

;;; hence even though you form the binary tree head the dot pair would display as a partial list

;;; you can have a list of dotted pairs like z 
;;; but not dotted pair of dotted pairs 

于 2021-04-18T02:52:13.947 回答