简体   繁体   English

可以使用 cons 来做 ((a. b). (c. d)) 吗?如果不是,任何其他方式或点对不能有这样的第二个元素?

[英]Can one use cons to do ((a . b) . (c . d)) and if not any other means or dot pair cannot have 2nd element like this?

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

;;; ;;; and if not any other means or dot pair cannot have 2nd element like this?如果没有任何其他方式或点对不能有这样的第二个元素?

Yes, you can.是的你可以。 And the language has a wonderful predicate called equal?语言有一个很棒的谓词叫做equal? which will allow you to test this:这将允许您对此进行测试:

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

And you can even write a little display function which will confirm this:你甚至可以写一点显示 function 来证实这一点:


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

And now现在

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

What this should all be telling you is that ((a. b). (c. d)) and ((a. b) c. d) are merely different ways of writing a structurally identical object .这应该告诉你的是((a. b). (c. d))((a. b) c. d)只是编写结构相同的 object 的不同方式

Pairs visualize differently based on their content.对根据其内容进行不同的可视化。 If the cdr of a pair contains the empty list it is a proper list an dthe dot and extra empty list is not shown:如果一对的cdr包含空列表,则它是一个正确的列表,并且不显示点和额外的空列表:

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

A pair that has pair as it's cdr can be visualized as a list element without the .具有 pair 作为cdr的对可以被可视化为没有. and extra parenthesis:和额外的括号:

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

These are just made so that we can have (1 2 3) displayed instead of (1. (2. (3. ()))) which is how it really is made.这些只是为了让我们可以显示(1 2 3)而不是(1. (2. (3. ()))) ,这就是它的真正制作方式。

If you were to not have a pair or a empty list in the cdr then it falls back to showing the dotted pair:如果您在cdr中没有一对或空列表,那么它会退回到显示点对:

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

In your example '((a. b). (c. d)) because there is a pair after a dot (eg. the cdr if the pair the visualization will remove the dot and one pair of parentheses and show it like ((a. b) c. d) . This is the only acceptable correct way for a REPL to display this even though both your example and the display will be read in as the same structure.在您的示例'((a. b). (c. d))因为在点之后有一对(例如cdr ,如果可视化将删除点和一对括号并将其显示为((a. b) c. d) . 这是 REPL 显示此内容的唯一可接受的正确方法,即使您的示例和显示都将作为相同的结构读入。

There is a similar issue with numbers.数字也有类似的问题。 In code you can use 10 , #xa and #o12 to get the number 10 and the value will have no idea what format is was read in as and only show the base 10 in the REPL.在代码中,您可以使用10#xa#o12来获取数字 10 并且该值将不知道读取的格式是什么,并且仅在 REPL 中显示以 10 为底的值。


;;; ```
;;; <- 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))



;;; ```

;;; ;;; and if not any other means or dot pair cannot have 2nd element like this?如果没有任何其他方式或点对不能有这样的第二个元素?

;;; ;;; possibly not可能不是


;;; 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 

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

相关问题 如果一对以空格字符结尾,则结果在两个元素之间将有一个点 - Scheme if pair end with a space char,the result will have one dot between two element 是否有任何其他语言具有像方案这样的内置理性类型? - Do any other languages have builtin rational types like scheme? 在Racket中,如果使用点表示法构造未引用的对,是否可以对第二个元素使用变量或表达式值? - In Racket, if an unquoted pair is constructed with the dot notation, is it possible to use a variable or an expression value for the second element? 如何从Scheme中的列表中删除倒数第二个元素? - How to delete the 2nd to last element from a list in Scheme? 获取第二个元素的list-ref错误 - list-ref error with getting 2nd element 我怎样才能以相反的方式使用CONS? - How can I use CONS in reverse way? 为什么你必须使用null来获得一个正确的方案列表? - Why do you have to cons with a null to get a proper list in scheme? 预计违反合同:数量? 给定:#<procedure:runtime> 参数位置:第二个其他参数...:1535481725945 - contract violation expected: number? given: #<procedure:runtime> argument position: 2nd other arguments...: 1535481725945 缺点返回列表并配对方案 - cons return a list and pair in scheme 如何拥有第二个元素是列表的对? - How to have a pair whose second element is a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM