SlideShare a Scribd company logo
1 of 16
Download to read offline
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/
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
!
• !(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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
C: A Humbling Language
C: A Humbling LanguageC: A Humbling Language
C: A Humbling Languageguestaa63aa
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Aslak Hellesøy
 
Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013trexy
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchinaguestcf9240
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
What's new in Perl 5.14
What's new in Perl 5.14What's new in Perl 5.14
What's new in Perl 5.14Andrew Shitov
 
Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009scweng
 
Scratching the surface of hunky-dory Elixir features
Scratching the surface of hunky-dory Elixir featuresScratching the surface of hunky-dory Elixir features
Scratching the surface of hunky-dory Elixir featuresAdam Hodowany
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaLin Yo-An
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scriptingDan Morrill
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Contextlichtkind
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...Viktor Turskyi
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajangJaricka Angelyd Marquez
 

What's hot (17)

Python Basic
Python BasicPython Basic
Python Basic
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
C: A Humbling Language
C: A Humbling LanguageC: A Humbling Language
C: A Humbling Language
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Embedding perl
Embedding perlEmbedding perl
Embedding perl
 
What's new in Perl 5.14
What's new in Perl 5.14What's new in Perl 5.14
What's new in Perl 5.14
 
Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009
 
Scratching the surface of hunky-dory Elixir features
Scratching the surface of hunky-dory Elixir featuresScratching the surface of hunky-dory Elixir features
Scratching the surface of hunky-dory Elixir features
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scripting
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
 
Lecture05(control structure part ii)
Lecture05(control structure part ii)Lecture05(control structure part ii)
Lecture05(control structure part ii)
 

Viewers also liked

Diagnostic Challenges In The Workshop
Diagnostic Challenges In The WorkshopDiagnostic Challenges In The Workshop
Diagnostic Challenges In The Workshopguest7838f0a
 
Charismatic, Goals-Focused Professional
Charismatic, Goals-Focused ProfessionalCharismatic, Goals-Focused Professional
Charismatic, Goals-Focused ProfessionalHonnibun
 
Free Software in the Catalonia Telecentre Network
Free Software in the Catalonia Telecentre NetworkFree Software in the Catalonia Telecentre Network
Free Software in the Catalonia Telecentre Networkframbla
 
Cochin Flower Show 2008
Cochin Flower Show 2008Cochin Flower Show 2008
Cochin Flower Show 2008Johnson C.J
 
Reform Primer Oct 2007 version
Reform Primer Oct 2007 versionReform Primer Oct 2007 version
Reform Primer Oct 2007 versionjmahan
 
Mechanics
MechanicsMechanics
MechanicsPhysEM
 
BleachBright Intro
BleachBright IntroBleachBright Intro
BleachBright IntroBill Scheidt
 
How to Play The Go Game
How to Play The Go GameHow to Play The Go Game
How to Play The Go GameMyles Nye
 
Ours To Share
Ours To ShareOurs To Share
Ours To ShareCarlo9951
 
Expocomm Mario Massone
Expocomm Mario MassoneExpocomm Mario Massone
Expocomm Mario Massoneguesta6d457b4
 
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin Romero
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin RomeroDidáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin Romero
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin RomeroGesvin Romero Moreno
 
NorthBridge Technology Overview
NorthBridge Technology OverviewNorthBridge Technology Overview
NorthBridge Technology OverviewAndrew Goodwin
 
Indian Scientific Heritage part1
Indian Scientific Heritage part1Indian Scientific Heritage part1
Indian Scientific Heritage part1versatile36
 
Manifesto2004 Internet
Manifesto2004 InternetManifesto2004 Internet
Manifesto2004 InternetKai Harris
 
Indian Scientific Heritage part2
Indian Scientific Heritage part2Indian Scientific Heritage part2
Indian Scientific Heritage part2versatile36
 

Viewers also liked (20)

Diagnostic Challenges In The Workshop
Diagnostic Challenges In The WorkshopDiagnostic Challenges In The Workshop
Diagnostic Challenges In The Workshop
 
Charismatic, Goals-Focused Professional
Charismatic, Goals-Focused ProfessionalCharismatic, Goals-Focused Professional
Charismatic, Goals-Focused Professional
 
Free Software in the Catalonia Telecentre Network
Free Software in the Catalonia Telecentre NetworkFree Software in the Catalonia Telecentre Network
Free Software in the Catalonia Telecentre Network
 
Cochin Flower Show 2008
Cochin Flower Show 2008Cochin Flower Show 2008
Cochin Flower Show 2008
 
Reform Primer Oct 2007 version
Reform Primer Oct 2007 versionReform Primer Oct 2007 version
Reform Primer Oct 2007 version
 
Mechanics
MechanicsMechanics
Mechanics
 
BleachBright Intro
BleachBright IntroBleachBright Intro
BleachBright Intro
 
Html css
Html cssHtml css
Html css
 
How to Play The Go Game
How to Play The Go GameHow to Play The Go Game
How to Play The Go Game
 
Beyond Real Time Web
Beyond Real Time WebBeyond Real Time Web
Beyond Real Time Web
 
Il Codice
Il CodiceIl Codice
Il Codice
 
Ours To Share
Ours To ShareOurs To Share
Ours To Share
 
Expocomm Mario Massone
Expocomm Mario MassoneExpocomm Mario Massone
Expocomm Mario Massone
 
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin Romero
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin RomeroDidáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin Romero
Didáctica del Error - Hacia un Aprendizaje para la Vida - Gesvin Romero
 
Week6
Week6Week6
Week6
 
NorthBridge Technology Overview
NorthBridge Technology OverviewNorthBridge Technology Overview
NorthBridge Technology Overview
 
Indian Scientific Heritage part1
Indian Scientific Heritage part1Indian Scientific Heritage part1
Indian Scientific Heritage part1
 
Manifesto2004 Internet
Manifesto2004 InternetManifesto2004 Internet
Manifesto2004 Internet
 
Logos
LogosLogos
Logos
 
Indian Scientific Heritage part2
Indian Scientific Heritage part2Indian Scientific Heritage part2
Indian Scientific Heritage part2
 

Similar to Iteration

F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 
Intro python
Intro pythonIntro python
Intro pythonkamzilla
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaMarko Gargenta
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de RailsFabio Akita
 
PERL Unit 6 regular expression
PERL Unit 6 regular expressionPERL Unit 6 regular expression
PERL Unit 6 regular expressionBinsent Ribera
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationAttila Balazs
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control EtcGeshan Manandhar
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
WTFin Perl
WTFin PerlWTFin Perl
WTFin Perllechupl
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottO'Reilly Media
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupJim Chang
 
PHP Basics for Designers
PHP Basics for DesignersPHP Basics for Designers
PHP Basics for DesignersMatthew Turland
 
Programming For Designers V3
Programming For Designers V3Programming For Designers V3
Programming For Designers V3sqoo
 
Procedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingProcedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingRich Price
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to ErlangGabriele Lana
 

Similar to Iteration (20)

Javascript
JavascriptJavascript
Javascript
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
Intro python
Intro pythonIntro python
Intro python
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de Rails
 
PERL Unit 6 regular expression
PERL Unit 6 regular expressionPERL Unit 6 regular expression
PERL Unit 6 regular expression
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
WTFin Perl
WTFin PerlWTFin Perl
WTFin Perl
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
Parse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful SoupParse The Web Using Python+Beautiful Soup
Parse The Web Using Python+Beautiful Soup
 
PHP Basics for Designers
PHP Basics for DesignersPHP Basics for Designers
PHP Basics for Designers
 
Programming For Designers V3
Programming For Designers V3Programming For Designers V3
Programming For Designers V3
 
Procedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debuggingProcedures, the Pop-11 stack and debugging
Procedures, the Pop-11 stack and debugging
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 

Recently uploaded

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 

Recently uploaded (20)

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 

Iteration

  • 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. 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. ! • !(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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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