Domanda

I'm studying scheme and I have just encountered my first problem:

(define x (cons (list 1 2) (list 3 4)))
(length x)
3

why the output is 3 and not 2? I have displayed x

((1 2) 3 4)

why is like that and not ((1 2) . (3 4)) ?

Thanks.

È stato utile?

Soluzione

Maybe it's easier to see this way.

You have:

(cons (list 1 2) (list 3 4))

If you

(define one-two (list 1 2))

you have

(cons one-two (list 3 4))

which is equivalent to

(cons one-two (cons 3 (cons 4 '())))

or

(list one-two 3 4)

which is

((1 2) 3 4)

Altri suggerimenti

List is the basic data structure of scheme. Cons is used for making a pair of objects. List is the chain of cons. eg. list (1 2 3 4) is same as (cons 1(cons 2(cons 3(cons 4 '())))). See the block pointer representation for make it clear

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top