Conditionals and Equivalence

7.1 Conditional Forms

(if (> 3 2) "yes" "no")

(define (sign n)
  (cond ((< n 0) 'negative)
        ((= n 0) 'zero)
        (else 'positive)))

;; cond with => (pass the test value to a procedure)
(cond ((assv 'b '((a 1) (b 2))) => cadr)
      (else 'not-found))              ; 2

(define (day-type day)
  (case day
    ((sat sun) 'weekend)
    ((mon tue wed thu fri) 'weekday)
    (else 'unknown)))

(case (* 2 3)
  ((2 3 5 7) 'prime)
  ((1 4 6 8 9) => (lambda (x) (list 'composite x)))   ; R7RS: => in case
  (else 'other))                      ; (composite 6)

(when (> 3 2) (display "yes") (newline))   ; R7RS: multiple body forms
(unless (> 1 2) (display "no"))
(and 1 2 3)                           ; 3
(and)                                 ; #t
(or #f 'x 'y)                         ; x
(or)                                  ; #f

7.2 Equivalence Predicates

Identity (pointer equality) Symbols, '(), booleans, same object

eq? + numbers (same exactness) + characters Numbers, characters (used by memv, assv, case)

Structural equality (recursive on pairs, vectors, strings, bytevectors) Lists, strings

string=?, char=?, symbol=?, boolean=?

(eq? 'a 'a)              ; #t
(eq? '() '())            ; #t
(eqv? 2 2)               ; #t
(eqv? 2 2.0)             ; #f
(eq? "abc" "abc")        ; unspecified (usually #f)
(equal? "abc" "abc")     ; #t
(equal? '(1 (2 #(3))) '(1 (2 #(3))))   ; #t
(eqv? 100000000000000000000 100000000000000000000)   ; #t (eq? may be #f for bignums)