tags:

views:

548

answers:

3
    > (eq? 1 1)
    #t
    > (eq? 1.1 1.1)
    #f
    > (= 1.1 1.1)
    #t

This is the interaction window in DrScheme. Could somebody please explain the difference between = and eq? in Scheme?

+1  A: 

first difference: eq? works with any pair of values, while = works with any number of numbers.

there are several other equivalence predicates. Most of them only accept exactly two parameters. = is defined in the 'numbers' chapter

Javier
+4  A: 

= compares numbers. eq? tests if the parameters represent the same data object in memory. eqv? should work in the second case as it tests same as eq? but tests primitives specially. More on equlivence predicates in scheme here.

JP Alioto
+2  A: 

I would guess that since

eq? evaluates to #f unless its parameters represent the same data object in memory;

and

Scheme stores inexact numbers (1.1) differently from exact numbers (1)

The two 1.1 arguments do not reside in the same place in memory and return #f for eq?

Wikipedia Reference

cmsjr