[racket] clarification for beginners please
On 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?
Welcome to Racket v5.3.4.5.
> (require math)
> (eq? (factorial 100) (factorial 100)) ;; small numbers are the same pointers in the system
#t
> (eq? (factorial 1000) (factorial 1000)) ;; big number are distinct pointers
#f
> (eqv? (factorial 1000) (factorial 1000)) ;; ... but they point to observably equal numbers, so eqv? uses = to compare them
#t
> (eqv? (cons 1 2) (cons 1 2)) ;; distinct pointers point to allocated structures, on the other hand, and eqv? compares pointers with pointer equality
#f
> (equal? (cons 1 2) (cons 1 2)) ;; so we need structural equality to find out whether they contain the same pieces down to the leaves
#t
> We have read the help menu verbage visible from DrRacket, but simply don't understand what it is saying.
See HtDP's section on extensional (equal?) vs intensional equality (eq?). As much as I like our help system, I don't think it's suitably for truly novice programmers.
> 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?"?
See beginning of message.