1

I am making a pure Lisp interpreter, and trying to write a reader to convert lists to cons pairs.

From what I've read lists are internally cons pairs such as this:

( 1 2 3 ) = (1.(2.(3.NIL)))

but I have no clue how to implement a nested list such as the following with cons pairs

( (1 2) (3 4 ) (5 6 ) )

How is this supposed to look like unabbreviated?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tipperary
  • 25
  • 1
  • 4
  • You may find this question and its answer (disclaimer, it's my answer) useful: [Dot notation in scheme](http://stackoverflow.com/questions/20216711/dot-notation-in-scheme). – Joshua Taylor Nov 09 '15 at 19:12

2 Answers2

2
CL-USER 40 > (sdraw::sdraw '( (1 2) (3 4 ) (5 6 ) ))

[*|*]------------------>[*|*]------------------>[*|*]--->NIL
 |                       |                       |
 v                       v                       v
[*|*]--->[*|*]--->NIL   [*|*]--->[*|*]--->NIL   [*|*]--->[*|*]--->NIL
 |        |              |        |              |        |
 v        v              v        v              v        v
 1        2              3        4              5        6
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
0

That would be

> '((1 . (2 . ())) . ((3 . (4 . ())) . ((5 . (6 . ())) . ())))
'((1 2) (3 4) (5 6))

or

? '((1 . (2 . nil)) . ((3 . (4 . nil)) . ((5 . (6 . nil)) . nil)))
((1 2) (3 4) (5 6))

See this question for a Scheme program (which is trivial to translate to Common Lisp) that prints a list as a dotted pair.

Community
  • 1
  • 1
uselpa
  • 18,732
  • 2
  • 34
  • 52