In SICP exercise 2.26 using DrScheme, why does cons return a list, instead of a pair of lists?

前端 未结 6 1315
北海茫月
北海茫月 2021-01-05 17:15

In SICP exercise 2.26, this Scheme code is given:

(define x (list 1 2 3))
(define y (list 4 5 6))

Then this cons call is given:

<         


        
6条回答
  •  春和景丽
    2021-01-05 17:24

    hey, i think you could think of it in this way;

    whenever there is a nil, there must be a pair of parenthesis, as follow:

    (cons 1 (cons 2 nil))--> (list 1 2)

    (let ((x (list 1 2 3)) (y (list 4 5 6))))

    1.(cons x y)--> (cons (cons 1 (cons 2 (cons 3 nil))) (cons 4 (cons 5 (cons 6 nil)))) here, the first nil stands for an end of a pair which could be expressed by parenthesis; whereas the second nil stands for the end of the whole pair which use another pair of parenthesis; so, ((1 2 3) 4 5 6)

    2.(list x y)-> (cons x (cons y nil); as we know the x contain a nil, so it must be (1 2 3); the second part contains two nils, so ((1 2 3) (4 5 6));

    the inner most nil means the outer most parenthesis;

    Hope it can help.

提交回复
热议问题