Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures CSC 358/458 5.1.2006. Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration.

Similar presentations


Presentation on theme: "Control Structures CSC 358/458 5.1.2006. Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration."— Presentation transcript:

1 Control Structures CSC 358/458 5.1.2006

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

3 Midterm Apologies! Question #3 had two errors the components of the polynomial are accessed wrong the example has an incorrect calculation

4 Midterm Average 40 358: 37 458: 42

5 Midterm answers

6 Lab #3 Eliza using text instead of list input file log what if we wanted to store the input, too?

7 Homework #4 Gin managing the game run-game user-draw-action user-choose-discard saving and loading

8 Meldp How does this work?

9 A better computer player Basic idea each state of the game the player has choices each choice leads to a different game state make the choice that leads to the most preferred state Three things needed move generation state generation state evaluation

10 Implementation Two choice points draw or upcard 2 possibilities discard 11 possibilities Simplest implementation local heuristic measure the relative benefit of each choice no long term plan no competitive analysis hand evaluator what is the "value" of a hand? choose the option that maximizes the hand value

11 Evaluation function Should be a value that gets higher as you get closer to winning Simple idea count the number of "mini-melds" card pairs that could contribute to a winning hand Better idea count the number of melds of each size weigh the bigger ones more weigh a gin hand highest

12 Fundamental Idea "State space search" Treat the problem as a set of states with actions = transition between states Choose the action that leads to the most preferred state More sophisticated look ahead into the set of states beyond one move

13 Control structures Basic evaluation not ordered Sequential Conditional Unconditional Iterative

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

15 Example make-iterator

16 Conditional if condition true optional false cond set of condition clause first true head, evaluate rest of clause

17 New conditionals when one-sided if with an implicit progn unless opposite

18 Example index-directory

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

20 Example numerology-lookup

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

22 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

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

24 Example length

25 prog Like tagbody but has a let-style variable definition clause allows return

26 Example length

27 Iteration Initial conditions Termination condition Termination result Transitions Body

28 Simplest dotimes (dotimes (var k result) body) initial var = 0 termination var = k transition var++

29 Example multiply

30 dolist (dolist (var lst result) body) initial var = car lst transition var = next car of lst termination lst processed

31 Example average

32 do Insight multiple variables often involved in iteration allow multiple updates per step (do ((var1 init1 update1)...) (exit-test result) body)

33 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

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

35 Example factorial dotimes as do dolist as do russian peasant method compare recursive

36 Exercise Write this function using do (defun my-count-if (pred lst) (if (null lst) 0 (let ((item (car lst))) (if (funcall pred item) (1+ (my-count-if pred (cdr lst))) (my-count-if pred (cdr lst)))))) Using do*

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

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

39 Examples even/odd sorting average

40 Non-local exit Sometimes program/function needs to exit error user request Can use return-from inside of a single function Difficult inside nested function calls

41 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

42 Example it-member-if

43 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?

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

45 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)))

46 Examples with do prime number generator input checking loop

47 Homework #5 My error No Homework this week Work on your project!


Download ppt "Control Structures CSC 358/458 5.1.2006. Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration."

Similar presentations


Ads by Google