Building Lists in Scheme

PUBLISHED ON JAN 4, 2019 — TYCS

Or, the differences between append, cons, & list

from sicp

  • 2.26: Suppose we define x and y to be two lists:

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

    What’s the output of the following?

    (append x y)
    ;; (1 2 3 (4 5 6))
    ;; but actually
    ;; (1 2 3 4 5 6)
    
    (cons x y)
    ;; ((1 2 3) (4 5 6))
    ;; but actually
    ;; ((1 2 3) 4 5 6)
    
    (list x y)
    ;; (1 2 3 4 5 6)
    ;; but actually
    ;; ((1 2 3) (4 5 6))
    
TAGS: SCHEME