Sol Bethany Reaves
Sol Bethany Reaves

Reputation: 387

Writing lists using only CONS command in Scheme

Using only the cons command in the Scheme Programming Language, how can one write nested lists such as

'(a b (x y (m)))?

Upvotes: 2

Views: 276

Answers (2)

Asqan
Asqan

Reputation: 4479

(define a "a")
(define b "b")
(define x "x")
(define y "y")
(define m "m")

(define example (cons a (cons b (cons (cons x (cons y (cons (cons m '()) '()))) '()))))

Resultaat:

   example
 '("a" "b" ("x" "y" ("m")))

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 222973

Hint: the car of a cons cell can be a cons cell too.

More particularly, the list you have is written in long form as:

(a . (b . ((x . (y . ((m . ()) . ()))) . ())))

Upvotes: 2

Related Questions