How to make ((AB). (CD)) in lisp

I am trying to figure out how to do this using cons:

((A . B) . (C . D))

where (A . B)and (C . D)are in each cell cons

I tried to do this (cons (cons 'a 'b) (cons 'c 'd)), but it gives me the following:

((A.B) C . D)

I also tried this: (cons (cons 'a 'b) (cons (cons 'c 'd) ()))but this gives me the following:

((A . B) (C . D))

Any idea how to achieve this?

+5
source share
4 answers

The first is what you want. They are equivalent. You can check the following:

1 ]=> (cons (cons 'a 'b) (cons 'c 'd))

;Value 11: ((a . b) c . d)

1 ]=> (car (cons (cons 'a 'b) (cons 'c 'd)))

;Value 12: (a . b)

1 ]=> (cdr (cons (cons 'a 'b) (cons 'c 'd)))

;Value 13: (c . d)

Remember that this is a cons cell. "Car" is the head of the list or the first half of the cons cell, and cdr is the rest of the list or the second element of the cons cell.

Another way to verify that they are equivalent:

1 ]=> '((a . b) . (c . d))

;Value 14: ((a . b) c . d)
+15

, , ((A . B) . (C . D)):

* '((a . b) . (c . d))

((A . B) C . D)

, Lisp , . , , , CDR .

, , SET-PPRINT-DISPATCH:

(set-pprint-dispatch 'cons
  (lambda (stream object)
    (format stream "(~W . ~W)" (car object) (cdr object))))
* '((a . b) . (c . d))

((A . B) . (C . D))
* (cons (cons 'a 'b) (cons 'c 'd))  ;The same object

((A . B) . (C . D))

, , , .

+7

, , - , Lisp. , cons, , cdr - cons cons. ((A . B) . (C . D)) " cons, (A . B), cdr - (C . D)". , (A . B), - C, - D ((A . B) C . D).

0
source

All Articles