2

I'm a newbie to scheme. I'm just confused about the difference of the following two list.

(define a '(1 2))
(define a '(1 . 2))

I think a equal to b, but

(equal? a b)

return #f to me. Any help will be greatly appreciated.

lujb
  • 67
  • 4

3 Answers3

4

The two aren't the same. The first is a normal list. In dotted notation it would look like this:

(1 . (2 . nil))

A normal list stores data in the car of a cons cell, and the cdr is only used to store a pointer to the next cons cell in the list, or Nil for the last cell in the list.

Your definition of a uses only one cons cell, with 1 in the car and 2 in the cdr.

If you drew them out graphically, they'd look like this:

enter image description here

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

The "dot" notation is used in Scheme and LISP to describe "improper lists", those that don't follow the standard list data definition. This question:

Functional Programming: what is an "improper list"?

... probably answers most of your questions. Let me know if there's anything this post doesn't answer.

Good luck!

Community
  • 1
  • 1
John Clements
  • 16,895
  • 3
  • 37
  • 52
0

They don't return equal because they are not the same data type. the first one with: (define a '(1 . 2)) is what is known as a pair. A list is a pair but not all pairs are lists. Lists are pairs that have a car and their cdr is a list. When you get the dot notation it means that the car of that pair is 1 and the cdr is 2. Since they aren't the same data type, they can't be equal.

bringel
  • 482
  • 5
  • 17