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

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 

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