390 likes | 543 Views
Control Structures. CSC 358/458 4.29.2003. Outline. Homework #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration Midterm. Homework #3. Biggest problems global variables in iterators not enough testing *SOME* == *WILD*. Homework #4.
E N D
Control Structures CSC 358/458 4.29.2003
Outline • Homework #3 • Homework #4 • Sequential structures • Conditional structures • Unconditional branching • Iteration • Midterm
Homework #3 • Biggest problems • global variables in iterators • not enough testing • *SOME* == *WILD*
Homework #4 • poly-to-string • string-to-poly • IR • save / load • alpha-char-p • Eliza • string input • logging
Proposals • OK • RR
Control structures • Basic • evaluation • not ordered • Sequential • Conditional • Unconditional • Iterative
Sequential • progn • evaluate expressions in order • return last • implicit progn • lambda, defun, cond • prog1 • evaluate expressions in order • return first • prog2 • return second
Example • make-iterator
Conditional • if • condition • true • optional false • cond • set of condition clause • first true head, evaluate rest of clause
New conditionals • when • one-sided if with an implicit progn • unless • opposite
Example • index-directory
Case • case • what switch should have been • any expression • matched against one or more possible matches
Example • numerology-lookup
Typecase • Select by the type of value • Type expressions are very flexible • numeric ranges • oddp/evenp • etc.
Unconditional Branching • Otherwise known as "goto" • From the programming dark ages • Not that useful • occasionally an efficiency can be achieved • mostly used to implement higher-level forms
tagbody • A progn • but bare keywords are interpreted as labels • (go label) • takes you to that label
Example • length
prog • Like tagbody • but • has a let-style variable definition clause • allows return
Example • length
Iteration • Initial conditions • Termination condition • Termination result • Transitions • Body
Simplest dotimes • (dotimes (var k result) body) • initial • var = 0 • termination • var = k • transition • var++
Example • multiply
dolist • (dolist (var lst result) body) • initial • var = car lst • transition • var = next car of lst • termination • lst processed
Example • average
do • Insight • multiple variables almost always involved in iteration (do ((var1 init1 update1)...) (exit-test result) body)
What happens • First time • initialize all variables • check for end point • execute the body • Other times • update variables • check for end point • execute body • If end-point test = true • return result
For loop (in C-derived languages) for (int i = 0; i < 10; i++) { ... block ... }
Example • factorial • dotimes as do • dolist as do • russian peasant method • compare recursive
Loop • Simple • (loop body) • infinite loop until RETURN • Complex • (loop ... craziness ...) • an embedded programming language • ignored by your book
Idea • Keywords that • declare variables • describe variable transitions/steps • describe accumulation of results • allow conditional operation • allow embedded blocks
Examples • even/odd sorting • average
Non-local exit • Sometimes program/function needs to exit • error • user request • Difficult inside nested function calls
Catch/Throw • catch • creates an identifier • code that uses it • throw • uses the identifier • to return a value for the whole expression • if throw is never called • normal value returned
Example • it-member-if
Problem (defun drill-hole (loc bit) (select bit) (move-to loc) (start-drill) (make-hole) (stop-drill)) • Now (catch 'error (drill-hole loc-a bit3)) • What happens if make-hole throws error?
Solution • unwind-protect (defun drill-hole (loc bit) (unwind-protect (progn (select bit) (move-to loc) (start-drill) (make-hole)) (stop-drill)))
with-open-stream (with-open-stream (s name :direction :input) body) • equivalent to (let ((s)) (unwind-protect (progn (setf s (open name :direction :input)) body) (close s)))
Iteration Practice • evaluate-polynomial • shuffle • mapiter • canonicalize-polynomial
Midterm • Open book • Combination • short answers • Lisp exercises • Material • everything except iteration