Upcoming SlideShare
×

Iteration

404 views

Published on

Published in: Technology, Spiritual
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Iteration

1. 1. A I P ro g ra m m ing Week Five: Iteration Richard Price rmp@ cs.bham.ac.uk www.cs.bham.ac.uk/~rmp/ www.cs.bham.ac.uk/internal/courses/ai-prog-a/
2. 2. R ec a p • Last week: – If … then … elseif… else… endif – Lists & pattern matching. Lvars input, name; ‘Hello what is your name? => Readline( - input; ;;; reads a line from the keyboard. )> If input matches ![Hello my is ?name] then [Hello ^name] => Else [Erm… hello] => Endif; 2
3. 3. ! • !(exclamation mark)is important. • Tells pop- 1 that there is a variable created 1 with lvars . • Without !matches does not work. If input matches ![Hello my is ?name] then 3
4. 4. C o m pa ris o n O pera to rs • Operators return <true> or <false>. • Define your own! define lessThanTen( aNumber)- result; > If aNumber < 1 0 then <true> - result; > else false - result; > endif; … If lessThanTen( aVariable)then … 4
5. 5. E rm … hello . Lvars input, name; ‘Hello what is your name? => Readline( - input; ;;; reads a line from the keyboard. )> If input matches ! [Hello my is ?name] then [Hello ^name] => Else [Erm… hello] => Endif; • What if our program is a natural language based database. • And needs that users name! 5
6. 6. I tera tio n • Often we want our code to repeat: – Until we get a satisfactory answer. – For a number of turns in a board game. – While certain conditions are met. • Pop- 1 does this using iteration . 1 – loops. 6
7. 7. R epea t repeat <number> times <code> endrepeat; repeat 5 times ‘Give me 5! => ’ endrepeat; * Give me 5! * * Give me 5! * * Give me 5! * * Give me 5! * * Give me 5! * 7
8. 8. Fo r for <variable> to <number> do <code> endfor; Lvars counter; for counter to 5 do counter 5 => endfor; • <variable> tracks the loop. • Starting at 1 . 8
9. 9. M o re Fo r lvars counter; for counter from - to 5 do 5 counter => endfor; • Will print from - to 5 inclusively. 5 for counter from 1 0 by 2 to 20 do counter => endfor; • Prints 1 0, 1 2, 1 4, 1 6, 1 8 and 20. 9
10. 10. W hile while <condition> do <code> endwhile; lvars total = 1 ; while lessThanTen( total)do total + random( - total; 5) > total => endwhile; • Adds a random number ( from 1 up to 5)to total. • Prints total until it’s greater than 1 0. 10
11. 11. W hile ‘Hello, what is your name?’ => lvars userName, correctInput = <false>, userInput; While not( correctInput)do readline( - userInput; )> if userInput matches ! my name is ?userName ==] then [== [Hello ^userName] => <true> - correctInput; > else [I am sorry, I did not catch that. W hat is your name?] => endif; endwhile; • Repeatedly asks the user for a suitable name. 11
12. 12. Fo r lis ts lvars exampleList = [1 2 3], listCounter = length( exampleList) ; for counter from 1 to listCounter do exampleList( counter)=> endfor; • Prints 1 , 2 then 3. • But so does: lvars itemInList, exampleList = [1 2 3] for itemInList in exampleList do itemInList => endfor; 12
13. 13. Fo r lis ts lvars itemInFirstList, itemInSecondList; Lvars mergedList = [ ], firstList = [1 2 3], secondList = [a b c]; for itemInFirstList, itemInSecondList in firstList, secondList do [^^mergedList ^itemInFirstList ^itemInSecondList] - mergedList; > endfor; mergedList => * [1 a 2 b 3 c] * • Ineffecient! 13
14. 14. D ec o ra ted B ra c k ets • Decorations % … % build lists via the stack. – When Pop- 1 sees the first % anything placed on the 1 stack is considered to be an item in a list. – When the second % all items on the stack are collected. – And inserted into a list in one go. lvars counter; [% for counter to 5 do counter; endfor; % ] => * [1 2 3 4 5] * 14
15. 15. Fo r lis ts [^^mergedList ^itemInFirstList ^itemInSecondList] - mergedList; > lvars itemInFirstList, itemInSecondList; Lvars firstList = [1 2 3], secondList = [a b c]; [% for itemInFirstList, itemInSecondList in firstList, secondList do itemInFirstList; ;;; leaves an item from the first list onto the stack. itemInSecondList; ;;; leaves an item from the second list onto the stack. endfor; % ] - merged_list; > merged_list => * [1 a 2 b 3 c] * 15
16. 16. I nfinite lo ops • At some point you will make an infinite loop. • Which will run forever. • At this point the only way to stop Pop- 1 is to hold 1 down control and press c . • If this doesn’t work type xkill & into the terminal. • And click on the window to kill. • Save your work often! 16