1

I have just started reading The Little schemer. I have some problem understanding some words.

In page 27 it says,

The Law of Eq?

The primitive eq? takes two arguments. Each must be a non-numeric atom."

And a footnote says: In practice, some numbers may be arguments of eq?

I am using racket-minimal as my scheme interpreter. It evaluates (eq? 10 10) to #t.

There are many similar type of problems in TOYS chapter.


What did the author mean by that must(marked as bold) and the footnote?

Shakil Ahamed
  • 587
  • 3
  • 18

2 Answers2

2

It's traditional to embed some primitive data types such as low integers and characters in the pointer itself making those datatypes eq? even when the data came to be in the source at different times / points in source. However, numbers can be any size so even if number upto a certain implementation dependent size at some point they will be to big for the pointer. When you try (eq? 10000000000 10000000000) it might be #f on 32 bits systems and #t in 64 bit systems while (eqv? 10000000000 10000000000) is #t in any system.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • what about `cons`. It says the second expression must be a list. but (car (cons 'a 'b)) evaluates to 'a. Can you explain it? – Shakil Ahamed May 21 '14 at 13:03
  • `cons` creates a pair. It's `car` and `cdr` can be of any type but only when the `cdr` is either `null?` or `list?` can the result be called a proper list. `list?` is when the `cdr` in the chain is `pair?` and the last is `null?`. A list that doesn't end with `null?` in it's `cdr` is called a dotted list, eg. `(a b . c)`. `(list? '(a b . c)) ; ==> #f` – Sylwester May 21 '14 at 13:44
  • +1 Got it. Found a 20 mintues tutorial on scheme which may be useful to read before going with TLS. Thanks – Shakil Ahamed May 21 '14 at 17:04
  • @silentboy The little Scheme approach is assimilation. It doesn't explain so much and expect you to eventually get it. It probably works best if this is your first programming language since then you don't look for mutation or for loops. I personally love the [SICP videos](https://www.youtube.com/watch?v=2Op3QLzMgSY&list=PL8FE88AA54363BC46), by Abelson and on of the authors of Scheme, Sussman. – Sylwester May 21 '14 at 17:44
  • I heard SICP is nice book. But, I decided to read TLS, The seasoned schemer before it. Is it a right path? I consider myself good at C. – Shakil Ahamed May 22 '14 at 02:18
1

Scheme's true identity predicate is eqv?. Eq? is an optimized version that is allowed to report #f instead of #t when applied to numbers, characters, or procedures. Most Scheme implementations of eq? do the right thing on small exact numbers (called "fixnums"), characters, and procedures, but fall down on larger numbers or numbers of other types.

So saying "MUST" means that you get only partly predictable results if you apply eq? to a number; the footnote means that in some cases (and this typically includes 10) you will get away with it. For details on what various Schemes actually do with fixnums, see FixnumInfo at the R7RS development site.

John Cowan
  • 1,497
  • 12
  • 13