University of California, Berkeley
EECS Department - Computer Science Division

CS3 Lecture 4 : Words, Sentences, True and False


Overview of today's lecture


Answers to questions that have come up

Review


Words and Sentences

Introduction

Selectors

function description

first word-or-sentence

 Return first letter of word, or first word of sentence

last word-or-sentence

 Return last letter or word, or last word of sentence

butfirst word-or-sentence
bf word-or-sentence

Return all but first letter of word, or word of sentence

butlast word-or-sentence
bl word-or-sentence

Return all but last letter of word, or word of sentence

 item n word-or-sentence

Return the nth letter of a word, or the nth word of a sentence

(define (second word-or-sentence)
   (first (butfirst word-or-sentence)))

(define (but-first-two word-or-sentence)
   (butfirst (butfirst word-or-sentence)))

Constructors

function description

word

 Join words into one big word

sentence
se

 Joins the arguments into a big sentence


True and False

Introduction

Predicates

Combining predicates with and, not and or

A B (and A B) (or A B) (not B)
#f #f #f #f #t
#f #t #f #t #f
#t #f #f #t #t
#t #t #t #t #f

If : How we make decisions...

cond: Writing conditionals

(define (get-cs3-grade score)
   (if (> score 390)
       'A+
       (if (> score 370)
           'A
           (if (> score 360)
               'A-
               'Did-not-get-an-A))))
(cond (cond1 actions1 ... )
      (cond2 actions2 ... )
               ...
      (condN actionsN ... )
      (else  else-actions ... ))
(define (get-cs3-grade score)
   (cond ((> score 390) 'A+)
         ((> score 370) 'A )
         ((> score 360) 'A-)
         (else 'Did-not-get-an-A)))
(define (get-cs3-grade score)
   (cond ((> score 390) 'A+)
         ((and (<= score 390) (> score 370)) 'A )
         ((and (<= score 370) (> score 360)) 'A-)
         (else 'Did-not-get-an-A)))

cond Example: Crossing an intersection

: (define (walk light city cops-present)
    (cond ((equal? city 'berkeley) 'strut)
          ((equal? light 'green) 'go)
          ((equal? light 'not-working) 'go-if-clear)
          ((and (equal? light 'flashing-red)
                 cops-present) 'wait)
          ((equal? light 'flashing-red) 'hurry)
          ((equal? light 'purple) 'stop-drinking)
          (else 'go-to-sleep)))
walk

: (walk 'green 'berkeley #t)
==> strut

: (walk 'green 'sf #t)
==> go

: (walk 'not-working 'sf #t)
==> go-if-clear

: (walk 'flashing-red 'sf #t)
==> wait

: (walk 'flashing-red 'sf #f)
==> hurry

: (walk 'purple 'sf #t)
==> stop-drinking

: (walk 'i-am-tired 'sf #t)
==> go-to-sleep


Summary

Next Time

Puzzle : Toggling frogs

Game : Fair shares and uneven pairs [BerlekampConwayGuy82]

References


WWW Maven: Dan Garcia (ddgarcia@cs.berkeley.edu) Send me feedback

Dan Garcia Berkeley Computer Science