CS151, Class 11: Conditionals Lab Overview: * Lab * Reflection Notes: * I may be late to class. Start without me. * Writeup deadline extended to Monday. REFLECTION * My documentation for eq? needs to be revised. Two values are eq? if and only if (1) they are both numbers and are the same number; (2) they are both symbols and are the same symbol; (3) they are both other types and share the same memory location. * BETTER DOCUMENTATION ON EQ: + Click on "Scheme Report" + Click on "Standard Procedures" + Click on "Equivalence" * You can think: + eq? and eqv? work for simple values, like symbols and number, they do not necessarily work on lists and strings. + equal? works on everything that eq? and eqv? work on, and also on lists and strings * How much testing should you do? A lot, with different kinds of inputs. E.g. + For numbers: 0, a variety of negative, a vareity of positive, complex, etc. + For lists: empty list, lists with repeated elements, etc. * An elegance issue + "Real" Schemers don't like (define in-range? (lambda (grade) (if (and (<= grade 100) (>= grade 0)) #t (if (or (< grade 0) (> grade 100)) #f #t)))) + (1) The second test is redundant (define in-range? (lambda (grade) (if (and (<= grade 100) (>= grade 0)) #t #f))) + (2) If the test returns true, the value is true; if the test returns false, the value is false. Why bother with the if? (define in-range? (lambda (grade) (and (<= grade 100) (>= grade 0)))) + (3) When you're doing a sequence of comparisons, use just <= with many arguments (define in-range? (lambda (grade) (<= 0 grade 100))) * Another elegance issue: + "Real" Schemers don't like (define symbol-or-list? (lambda (val) (cond ((symbol? val) #t) ((list? val) #t) (else #f)))) + They also don't like (define symbol-or-list? (lambda (val) (if (or (symbol? val) (list? val)) #t #f))) + They do like (define symbol-or-list? (lambda (val) (or (symbol? val) (list? val)))) + They'd really prefer (define symbol-or-list? (or symbol? list?)) BUT THAT DOESN'T WORK