University of California, Berkeley
EECS Department - Computer Science Division

CS3 Lecture 21 : Input / Output


Overview of today's lecture


Review

Trees


Strings

: "Go Bears"
==>

: (string? "Go Bears")
==>

: (string-length "Go Bears")
==>


Sequential Programming

Effect, Sequence and State

Input / Output

"This is the point at which there is usually some ..."

Output functions

: (display 'a-symbol)
==>

: (write 'a-symbol)
==>

: (display "a string")
==>

: (write "a string")
==>

for-each - the mapping function for side-effects

: (for-each abs '(-1 2 -3.4 -5/6))
side-effects ==>
==>

: (for-each msword-2050-command 
            '(launch open-term-paper print-it crash-as-usual))
side-effects ==>
==>
: (for-each robot-command 
            '(write         prepare massage))
            '(my-term-paper lunch   feet))
side-effects ==>
==>
;; : (print-list-one-elt-per-line '("Cal Bears" (we are) Number 1)) ==>
;; Cal Bears
;; (we are)
;; number
;; 1
;; done!

(define (print-list-one-elt-per-line L)
==>
==>
==>
==>

: (print-list-one-elt-per-line '("Cal Bears" (we are) Number 1))
==>
==>
==>
==>
==>

Output: What about if? (solution: begin)

;; : (big-game-winner-prediction 'cal)
;; ==P Party on! You were right that
;; ==P Cal will win the Big Game!
;; ==> done!
;;
;; : (big-game-winner-prediction 'stanfurd)
;; ==P Bzzt! Thanks for trying...
;; ==P stanfurd will not win the Big Game, Cal will!
;; ==> done!
;;
;; : (big-game-winner-prediction 'eat-my-shorts)
;; ==P Bzzt! Thanks for trying...
;; ==P eat-my-shorts will not win the Big Game, Cal will!
;; ==> done!

(define (big-game-winner-prediction team)
  (if (equal? team 'cal)

       ;; They predicted Cal
       (display "Party on! You were right that")
       (newline)
       (display "Cal will win the Big Game!")

       ;; They didn't predict Cal
       (display "Bzzt! Thanks for trying...")
       (newline)
       (display team)
       (display " will not win the Big Game, Cal will!"))
  (newline)
  'done!)

Input: read

: (read)
42
==>

: (read)
cs3
==>

: (read)
cs3 is the best class
==>

: (read)
"cs3 is the best class"
==>

: (read)
(cs3 is the best class)
==>

: (read)
(+ 1 1) (+ 150 150)
==>

Input example: get-valid-input

;; read until input is in valid-input
;; print intro message to user first - prompt

(define (get-valid-input valid-input message)
  (display message)
  (display " ")
  (let ((input (read)))
    (cond ((member input valid-input)
            input)
          (else (display "Bad value entered: ")
                (display input)
                (newline)
                (get-valid-input valid-input message)))))

: (get-valid-input '(yes yep sure uh-huh) "Is CS3 cool?")
==>
==>
==>
==>
==>
==>


Summary

Next Time

Puzzle : Y3K NCAA Basketball Tournament

Game : Seega ["Pentagames" by Pentagram, Fireside Publishing, 1990]

         
         
   

*
   
         
         

Blank Board

   

X
   
   

O
   
 

X

*

O

X
   

O
   
   

X
   

Example Game (X to move)