;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; U. C. Berkeley ;; ;; EECS Computer Science Division ;; ;; CS3 Lecture 04 Answers ;; ;; (Words, Sentences, True & False) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;; ;; WORDS AND SENTENCES ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;; ;; Quoting ;;;;;;;;;; ;;; Expressions, as we've seen (define (square x) (* x x)) ;; ==> square square ;; ==> #[procedure square] cal ;; ==> *** ERROR -- Unbound variable: cal (+ 1 2) ;; ==> 3 (this is a sentence) ;; ==> *** ERROR -- Unbound variable: this ;;; But what if I don't want to evaluate it, ;;; but just take it literally? The answer: quote (quote square) ;; ==> square (quote cal) ;; ==> cal (quote (+ 1 2)) ;; ==> (+ 1 2) (quote (this is a sentence)) ;; ==> (this is a sentence) ;;; We use an abbreviation for it: ' 'square ;; ==> square (quote cal) ;; ==> cal '(+ 1 2) ;; ==> (+ 1 2) '(this is a sentence) ;; ==> (this is a sentence) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Selectors (of words and sentences) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (first 501) ;; ==> 5 (first 'cal) ;; ==> c (first '(cal is number 1)) ;; ==> cal (first (first '(cal is number 1))) ;; ==> c (last 501) ;; ==> 1 (last 'cal) ;; ==> l (last '(cal is number 1)) ;; ==> 1 (butfirst 501) ;; ==> "01" ;; This is a number, even though it's in double-quotes (* 795 (butfirst 501)) ;; ==> 795 (bf 'cal) ;; ==> al (bf '(cal is number 1)) ;; ==> (is number 1) (butlast 501) ;; ==> 50 (bl 'cal) ;; ==> ca (bl '(cal is number 1)) ;; ==> (cal is number) (item 3 795) ;; ==> 5 (item 2 'cal) ;; ==> a (item 1 '(cal is number 1)) ;; ==> cal ;; second ;; ;; Return the second element of word-or-sentence ;; (define (second word-or-sentence) (first (butfirst word-or-sentence))) (second '(cal is number 1)) ;; ==> is (second '795) ;; ==> 9 ;; but-first-two ;; ;; Return all but the first two elements of word-or-sentence ;; (define (but-first-two word-or-sentence) (butfirst (butfirst word-or-sentence))) (but-first-two '(cal is number 1)) ;; ==> (number 1) ;; How do I get m from '(cal is number 1)? ==> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Constructors (of words and sentences) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (word 'owha "tagoo" 'siam 01 'huh?) ;; ==> owhatagoosiam1huh? (word 9 02 10) ;; ==> 9210 (word 9 "02" 10) ;; ==> 90210 (sentence 'cal 'is 'number 1) ;; (cal is number 1) (se '(cal is) (word 'num 'ber) '(1)) ;; (cal is number 1) ;;;;;;;;;;;;;;;;; ;; TRUE AND FALSE ;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;; ;; Predicates ;;;;;;;;;;;;; (member? 'i 'team) ;; ==> #f (before? 'aardvark 'zebra) ;; ==> #t (< 1 2 3) ;; ==> #t ;;;;;;;;;;;;;;;;; ;; And / Or / Not ;;;;;;;;;;;;;;;;; (and #t #f) ;; ==> #f (or #t #f) ;; ==> #t (and 1 2) ;; ==> 2 (or 1 2) ;; ==> 1 (and #f #f 3) ;; ==> #f (or #f #f 3) ;; ==> 3 (not #f) ;; ==> #t (not #t) ;; ==> #f (not 3) ;; ==> #f ;;; Short-circuit example (/ 6 0) ;; ==> *** ERROR -- Division by zero (and #t (/ 6 0)) ;; ==> *** ERROR -- Division by zero (and #f (/ 6 0)) ;; ==> #f (or #f (/ 6 0)) ;; ==> *** ERROR -- Division by zero (or #t (/ 6 0)) ;; ==> #t ;;; Test if number is between 0 and 100 (define (num-between-0-and-100 num) ;; ==> (and (> number 0) (< number 100)) (num-between-0-and-100 -5) ;; ==> #f (num-between-0-and-100 32) ;; ==> #t (num-between-0-and-100 105) ;; ==> #f ;;; Could this be done more efficiently ;;; (i.e., without ands and ors)? (define (num-between-0-and-100-efficient num) ;; ==> (< 0 num 100) ;;;;; ;; IF ;;;;; (/ 6 3) ;; ==> 2 (/ 6 0) ;; ==> *** ERROR -- Division by zero (define (safe-divide num1 num2) (if (= 0 num2) 'infinity (/ num1 num2))) (safe-divide 6 0) ;; ==> infinity (safe-divide 6 3) ;; ==> 2 ;;; Why is if a special form? ;;; What would happen in safe-divide if it wasn't? ;;; ==> It would evaluate (/ num1 num2) regardless of whether ;;; it was going to be the branch and crash when num2 was zero. ;;; What's wrong with this? ;;; What does the following return and what should it return? (safe-divide 0 0) ;; This should return ==> undefined ;; ==> infinity ;;; Let's add a second test for num1 = 0 (define (safe-divide-fixed num1 num2) (if (zero? num2) (if (zero? num1) 'undefined 'infinity) (/ num1 num2))) (safe-divide-fixed 0 0) ;; ==> undefined ;;; If is composable ;;; ;;; Look at the two examples below. They perform the same ;;; computation. Note the use of if in the second example. (define (grade-sentence name pass-or-fail) (if (equal? pass-or-fail 'pass) (se 'Hi name '(you have passed the course)) (se 'Hi name '(you have miserably failed the course)))) (define (grade-sentence name pass-or-fail) (se 'Hi name '(you have) (if (equal? pass-or-fail 'pass) 'passed '(miserably failed)) '(the course))) (grade-sentence '(dr einstein) 'pass) ;; ==> (hi dr einstein you have passed the course) (grade-sentence 'bart 'fail) ;; ==> (hi bart you have miserably failed the course) ;;;;;;; ;; COND ;;;;;;; ;; Want to write answer, which when given a sentence ;; responds as follows: ;; ;; : (answer '(are you a computer)) ;; (yes i am a computer) ;; ;; : (answer '(am i smart)) ;; (yes you are smart) ;; ;; Otherwise ;; ;; : (answer '(this is a random sentence)) ;; (i do not understand) (define (answer sent) (cond ((and (equal? (first sent) 'are) (equal? (second sent) 'you)) (se '(yes i am) (but-first-two sent))) ((and (equal? (first sent) 'am) (equal? (second sent) 'i)) (se '(yes you are) (but-first-two sent))) (else '(i do not understand)))) (answer '(are you a macintosh)) ;; ==> (yes i am a macintosh) (answer '(am i in bear territory)) ;; ==> (yes you are in bear territory) ;;; Now let's break it! (answer '( are you going with me to the movies? )) ;; ==> (yes i am going with me to the movies?) ;; Let's now modify answer so that it reads ;; : (are subjects description) ;; (yes subjects are description) ;; ;; e.g. ;; : (answer '(are people here)) ;; (yes people are here) (define (answer sent) (cond ((equal? (se (first sent) (second sent)) '(are you)) (se '(yes i am) (but-first-two sent))) ((equal? (first sent) 'are) (se 'yes (second sent) 'are (but-first-two sent))) ((equal? (first sent) 'am) (se '(yes you are) (but-first-two sent))) (else '(i do not understand)))) (answer '(are we the best class ever)) ;; ==> (yes you are the best class ever) (answer '(are cats the cutest animals)) ;; ==> (yes cats are the cutest animals) ;; Now let's break it! ;; (You should think of other ways to break it!) (answer '(are dogs and cats friendly)) ;; ==> (yes dogs are and cats friendly)