1 / 39

Control Structures

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.

brier
Télécharger la présentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures CSC 358/458 4.29.2003

  2. Outline • Homework #3 • Homework #4 • Sequential structures • Conditional structures • Unconditional branching • Iteration • Midterm

  3. Homework #3 • Biggest problems • global variables in iterators • not enough testing • *SOME* == *WILD*

  4. Homework #4 • poly-to-string • string-to-poly • IR • save / load • alpha-char-p • Eliza • string input • logging

  5. Proposals • OK • RR

  6. Control structures • Basic • evaluation • not ordered • Sequential • Conditional • Unconditional • Iterative

  7. Sequential • progn • evaluate expressions in order • return last • implicit progn • lambda, defun, cond • prog1 • evaluate expressions in order • return first • prog2 • return second

  8. Example • make-iterator

  9. Conditional • if • condition • true • optional false • cond • set of condition clause • first true head, evaluate rest of clause

  10. New conditionals • when • one-sided if with an implicit progn • unless • opposite

  11. Example • index-directory

  12. Case • case • what switch should have been • any expression • matched against one or more possible matches

  13. Example • numerology-lookup

  14. Typecase • Select by the type of value • Type expressions are very flexible • numeric ranges • oddp/evenp • etc.

  15. 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

  16. tagbody • A progn • but bare keywords are interpreted as labels • (go label) • takes you to that label

  17. Example • length

  18. prog • Like tagbody • but • has a let-style variable definition clause • allows return

  19. Example • length

  20. Iteration • Initial conditions • Termination condition • Termination result • Transitions • Body

  21. Simplest dotimes • (dotimes (var k result) body) • initial • var = 0 • termination • var = k • transition • var++

  22. Example • multiply

  23. dolist • (dolist (var lst result) body) • initial • var = car lst • transition • var = next car of lst • termination • lst processed

  24. Example • average

  25. do • Insight • multiple variables almost always involved in iteration (do ((var1 init1 update1)...) (exit-test result) body)

  26. 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

  27. For loop (in C-derived languages) for (int i = 0; i < 10; i++) { ... block ... }

  28. Example • factorial • dotimes as do • dolist as do • russian peasant method • compare recursive

  29. Loop • Simple • (loop body) • infinite loop until RETURN • Complex • (loop ... craziness ...) • an embedded programming language • ignored by your book

  30. Idea • Keywords that • declare variables • describe variable transitions/steps • describe accumulation of results • allow conditional operation • allow embedded blocks

  31. Examples • even/odd sorting • average

  32. Non-local exit • Sometimes program/function needs to exit • error • user request • Difficult inside nested function calls

  33. 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

  34. Example • it-member-if

  35. 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?

  36. Solution • unwind-protect (defun drill-hole (loc bit) (unwind-protect (progn (select bit) (move-to loc) (start-drill) (make-hole)) (stop-drill)))

  37. 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)))

  38. Iteration Practice • evaluate-polynomial • shuffle • mapiter • canonicalize-polynomial

  39. Midterm • Open book • Combination • short answers • Lisp exercises • Material • everything except iteration

More Related