2

I am currently playing around with LISP. Everything is fine, but I can't understand the following issue.

I have the this append-operation:

(define (append l1 l2)
   (if (eq? l1 null)
      l2
      (cons (first l1)
            (myappend (rest l1) l2))))

I use it like this:

(myappend (cons (cons 1 2) null) '(4 5))

And the result in Racket is:

 '((1 . 2) 4 5)

But why? In my oppinion it should be '(1 2 4 5), because cons returns a list and myappends appends two lists. Can anybody help me? What is LISP doing?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
  • 3
    You seem to be using some nonstandard features peculiar to Racket. In Lisp (ANSI Common Lisp) lists are terminated by the symbol `nil`. In standard Scheme (of which Racket is evidently a dialect), lists are not terminated by a symbol. They are terminated by an empty list object which is written `()` (and which must be quoted when used as an expression: `'()`). In Scheme you use `(null? x)` to test whether `x` is the empty list, not `(eq x null)`; there is no predefined `null`. In Common Lisp, it's `(null x)` or `(not x)` or `(eq x nil)`. – Kaz Apr 09 '12 at 19:27

4 Answers4

11

cons returns a dotted pair, not necessarily a list.

(cons 1 2) returns (1 . 2)

(cons 1 null) returns (1)

(cons 1 (cons 2 null)) returns (1 2)

Sam Tobin-Hochstadt
  • 4,983
  • 1
  • 21
  • 43
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • Ok, thank you. But way (cons 1 (cons 2 null)) doesn't return (1 .(2))? – Thomas Uhrig Apr 07 '12 at 17:37
  • 4
    @Thomas: Both expressions represent the same structure; the convention is to prefer list notation instead of dotted pair notation whenever possible. If some cons cell in the structure has a cdr that's a non-null atom (as in your question), dotted pair notation will be required because the structure is not a list. – Jim Lewis Apr 07 '12 at 17:47
  • @ThomasUhrig [This answer](http://stackoverflow.com/a/16379759/1281433) (disclaimer: it's mine) has more about the printed representation of cons cells. – Joshua Taylor Oct 14 '13 at 15:31
4

A (cons 1 2) will return an object whose first pointer (car) points to 1, and the other (cdr) points to 2, that's why it get printed in the dot-pair fashion.

Also you may want to understand deeper, I will recommend you read the CL: gentle introduction to symbolic computation, "6.4. Comparing CONS, LIST, AND APPEND", which explained these topics really well.

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
2

Try what (cons 1 2) returns. Is it a list?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
1

to @ThomasUhrig : the following info might help you.

Although we are talking about Lisp language here, I notice that a line from Page 8 and 9 of a famous Book named "The Little Schemer (4th edition)" help me understand the 2 puzzling facts altogether:

    Why (cons 1    2) does not look like '(1 2)?
    Why (cons 1 '(2)) does     look like '(1 2)?
    ----
    > (cons 1 2)
    (1 . 2)
    > (cons 1 '(2))
    (1 2)
    > '(1 2)
    (1 2)

Just read the "The Laws of Cons":

The primitive cons takes 2 arguments.

The 2nd argument to cons must be a list.

The result is a list.

In practice: (cons A B) works for all values A and B, And

(car (cons A B)) = A

(cdr (cons A B)) = B

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
pimgeek
  • 259
  • 1
  • 6
  • 17
  • 1
    The link to the PDF doesn't work anymore. I can still find copies easily with Google, but it appears to still be on sale in places; I'm not sure whether it's supposed to be freely distributed. That said, the relevant "Law of Cons" is available in the Google Books [preview](http://books.google.com/books?id=xyO-KLexVnMC&lpg=PA9&dq=little%20schemer%20%22law%20of%20cons%22&pg=PA9#v=onepage&q=little%20schemer%20%22law%20of%20cons%22&f=false). – Joshua Taylor Dec 14 '13 at 20:39