Com S 342 meeting -*- Outline -*- * logical connectives (SICP, p. 19; Little Schemer, chapter 2) ------------------------------------------ LOGICAL CONNECTIVES problem: verbosity (deftype member? (forall (T) (-> (T (list-of T)) boolean))) (define member? (lambda (e ls) (if (not (null? ls)) (if (equal? e (car ls)) #t (member? e (cdr ls))) #f))) solution: (define member? (lambda (e ls) TERMS short-circuit evaluation conditional-evaluation ------------------------------------------ ... (and (not (null? ls)) (or (equal? e (car ls)) (member? e (cdr ls)))))) Q: Can or evaluate all its arguments? What would happen? no, it would loop bring out importance of short-circuit evaluation ------------------------------------------ SYNTAX OF LOGICAL CONNECTIVES ::= SEMANTICS ------------------------------------------ ... (or {}*) | (and {}*) | ... ... (or ) = #f (or e1 e2 ...) = (if e1 #t (or e2 ...)) (and ) = #t (and e1 e2 ...) = (if e1 (and e2 ...) #f) Actually, in Scheme anything not #f is true, so or is a bit more complex in reality, see the book Q: What's this like in other languages? in C? in Pascal? || and && in C, not | and & Pascal has no equivalent cor, cand in Algol 68? and_then, or_else in Ada ------------------------------------------ FOR YOU TO DO Rewrite the following using "and" and "or" (deftype foo? (-> (datum) boolean)) (define foo? (lambda (x) (if (pair? x) (if (number? (car x)) #t (number? (cdr x))) #f))) Define the predicate (deftype two-elem-list? (-> (datum) boolean) to test whether a datum is a list of length two. ------------------------------------------