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.

3 Answers 3

5

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

0
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!

1
  • Thank you very much. I just have known another concept of fp.
    – lujb
    Mar 31, 2011 at 6:09
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.