[racket] clarification for beginners please

From: Marco Morazan (morazanm at gmail.com)
Date: Thu Apr 25 16:45:41 EDT 2013


Perhaps a more pedestrian explanation might help. There is a difference
between two items having the same value and two items being the same item.

equal? tests if its two items have the same value.

eq? tests if two items are the same item.

Using your example:

(define A (make-posn 4 5))
(define B (make-posn (+ 3 1) (- 6 1)))

Both A and B are items (in this case posns) that have the same value. Thus,
(equals? A B) is true.

A and B, however, are not the same item (they are two different posns that
happen to have the same value). Thus, (eq? A B) is false.


(define C B)

Now, C and B are the same item (the same posn). Thus, (eq? C B) is true. As
C and A are not the same posn, we have (eq? C A) is false. As you would
expect (equal? C A) is true (because the have the same value).

eq? returning true suffices to know that equal? returns true (if two items
are the same item, then clearly they have the same value). equal? returning
true does not suffice to know what eq? returns (given that two different
items may or may not have the same value).

This difference is not relevant for most of HtDP, because it is not until
your reach the material with assignment that you need to understand/know if
two items are the same item or not. Before the assignment material, you are
always testing for value equality.

Finally, eqv? is not used in HtDP and my advice would be to ignore its
existence with beginners.

I hope this helps.


On Thu, Apr 25, 2013 at 2:31 PM, Ford, Joe <jford at hollandhall.org> wrote:

> I have a group of high school students with a question... can someone
> please explain to beginner Racket users the differences between these three
> boolean functions:   eq?   equal?   eqv?
> We have read the help menu verbage visible from DrRacket, but simply don't
> understand what it is saying.  Maybe that's lack of context or
> vocabulary... but we're struggling a bit.  To test simple variations of
> application, we wrote some simple code (shown below) but don't understand
> why the results are what they are:
> (define FOUR "four")
> (define A (make-posn 4 5))
> (define B (make-posn (+ 3 1) (- 6 1)))
> "-------------"
> (equal?  FOUR  "four")
> (equal?  4  (+ 1 3))
> (equal?  4 (posn-x (make-posn 4 3)))
> (equal? A B)
> "-------------"
> (eq?  FOUR  "four")
> (eq?  4  (+ 1 3))
> (eq?  4 (posn-x (make-posn 4 3)))
> (eq? A B)
> "---------------"
> (eqv?  FOUR  "four")
> (eqv?  4  (+ 1 3))
> (eqv?  4 (posn-x (make-posn 4 3)))
> (eqv? A B)
> Why in the world would the above-defined A and B be considered "equal?"
> but not "eq?" or "eqv?"?
> --
> *Joe Ford
> Technology Resources, Scheduling & Yearbook
> Holland Hall
> jford at hollandhall.org
> *
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users




Have a´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·´ * wonderful day! :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130425/f9ee51f9/attachment.html>

Posted on the users mailing list.