SlideShare a Scribd company logo
1 of 21
Download to read offline
AI Programming
                             Week Four:
              Conditionals, Lists and Pattern Matching




     Richard Price
     rmp@ cs.bham.ac.uk
     www.cs.bham.ac.uk/~rmp/




www.cs.bham.ac.uk/internal/courses/ai-prog-a/
C o nditio na ls
• If <this statement is true> then …

if <condition> then
   <code>
endif;


lvars myVariable = 1 0;
if myVariable = 1 0 then
   ‘My Variable is equal to 1 0’ =>
endif;




                                       2
C o m pa ris o n O pera to rs
•   =       (
            equals)
•   <       (
            Less than)
•   >       (
            Greater than)
•   <=      (
            Less than or equal to)
•   >=      (
            Greater than or equal to)
lvars myVariable = 1 0;
myVariable = 1 0 =>
* <true>
 *



                                        3
I f … a nd/or …
if <condition> and <condition> then
   <code>
endif;


lvars myVariable = 1 0;
if myVariable < 1 1 and myVariable > 9 then
   ‘My Variable is 1 0! =>
                      ’
endif;


lvars myPet = ‘duck’
if myPet = ‘duck’ or myPet = ‘budgie’ then
   ‘My pet is a bird! =>
                    ’
endif;
                                              4
I f … then … els e …
if <condition> then
   <code>
else
   <code>
endif;


lvars myPet = ‘cat’;
if myPet = ‘duck’ or myPet = ‘budgie’ then
   ‘My pet is a bird! =>
                    ’
Else
   ‘My pet is not a bird :( =>
                          ’
endif;


                                             5
I f … then … els eif … els e …
lvars myVariable = 1 1 ;
if myVariable < 1 1
   ‘My Variable is less than 1 1 ! =>
                                 ’
elseif myVariable > 1 1
   ‘My Variable is greater than 1 1 ! =>
                                    ’
else
   ‘My Variable is 1 1 ! =>
                       ’
endif;




                                           6
I f … then … els eif … els e …
define coffeeBreak(
                  bored, money)- response;
                                >
   if bored = ‘true’ then
           if money >= 5 then
                  ‘Hurrah!Coffee and cake! - response;
                                         ’>
           elseif money > 2
                  ‘Coffee! - response;
                         ’>
           else
                  ‘Oh noes… water… ’ - response;
                                      >
           endif;
    else
           ‘W ork, work’ - response;
                          >
   endif;
enddefine;
coffeebreak(
           ‘true’, 5)=>
                                                         7
L is ts
• Lists hold pieces of data
     – Of any type.
     – In sequence.

lvars myList [a b c];
myList =>
* [a b c]
 *


myList( =>
      2)
*b
*



                              8
L is ts in lis ts
• Nested lists like:

lvars myNestedList = [a b [c d] [e f]];


• Can be printed out using the ‘pretty print arrow’:

myNestedList ==>
* [a b
 *
         [c d]
         [e f]]




                                                  9
A dding to L is ts
• The operators:
    –^ ( hat)
    – ^^ (
         double hat)

lvars myList = [I have a pet], insert = ‘dog’;
[I have a pet ^insert] =>


* I have a pet dog.
 *




                                                 10
^ o r ^^
lvars insert = [and another list];


[I have a list ^insert] =>
* [I have a list [and another list] ]
 *


[I have a list ^^insert] =>
* [I have a list and another list]
 *


• ^^ loses the square brackets [ ].
• Experiment with this!


                                        11
L ink ed L is ts
• Pop- 1 uses linked lists.
      1
• Sequence of connected nodes.
• Each node consists of:
  – A pointer.
  – And the data item.
  – Lists start with a header
  – End with a footer.
  – End of list’s pointer is null.


                                     12
L ink ed L is ts
• Variables simply point to the head of the list.




                                            13
L ink ed L is ts
• Copying lists




copydata(
        myList)- myOtherList;
                >
                                14
C ha ng ing a nd A dding L is ts




                                   15
L is t func tio ns
• Teach …
  –   ><        (
                concatenation)
  –   ::        (
                head insertion)
  –   length()
  –   hd( )
  –   tl()
  –   rev( )
  –   shuffle()
  –   oneof( )
  –   sort()


                                  16
M a tc hes
• Pop- 1 allows us to flexibly match lists.
      1

[a b c d] matches [a b c d] =>
* <true>
 *


• Matches is an operator returning <true> or
  <false>




                                              17
P a ttern M a tc hing
• Like ^ and ^^ we can use = and == on lists.
• = matches a single element in a list.

[a b c d] matches [= b c d] =>
* <true>
 *


• == matches 0 or more elements.

[a b c d] matches [== b c d] =>
[a b c d] matches [a == d] =>
[a b c d] matches [a b c ==] =>
[a b c d] matches [a b c d ==] =>

                                                18
P a ttern M a tc hing
• And combine them:

[a b c d] matches [= b ==] =>
* <true>
 *


• What about?

[a b c d] matches [= b = =] =>
[a b c d] matches [==] =>
[a b c d] matches [= = = =] =>
[a b c d] matches [== == = = = = ==] =>
[a b c d] matches [a == c = =] =>
                                          19
P a ttern M a tc hing
• Like ^, ^^, = and == we can use ? and ??:

[a b c d] matches [= b ?anItem ==];
anItem =>
*c
*


[a b c d] matches [a b ??items];
Items =>
* [c d]
 *




                                         20
P a ttern M a tc hing

Lvars input, name;
‘Hello what is your name? =>
Readline( - input; ;;; reads a line from the keyboard.
        )>


If input matches !
                 [Hello my name is ?name] then
   [Hello ^name] =>
Else
   [Erm… hello] =>
Endif;




                                                         21

More Related Content

Viewers also liked

Indian Scientific Heritage part2
Indian Scientific Heritage part2Indian Scientific Heritage part2
Indian Scientific Heritage part2
versatile36
 
Bid corporate presentation
Bid corporate presentationBid corporate presentation
Bid corporate presentation
dlawrence
 
Indian Scientific Heritage part1
Indian Scientific Heritage part1Indian Scientific Heritage part1
Indian Scientific Heritage part1
versatile36
 
Variables & Expressions
Variables & ExpressionsVariables & Expressions
Variables & Expressions
Rich Price
 
FP2003 ch 3~5
FP2003 ch 3~5FP2003 ch 3~5
FP2003 ch 3~5
楊 騏
 
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
Rich Price
 

Viewers also liked (20)

Indian Scientific Heritage part2
Indian Scientific Heritage part2Indian Scientific Heritage part2
Indian Scientific Heritage part2
 
Bid corporate presentation
Bid corporate presentationBid corporate presentation
Bid corporate presentation
 
Indian Scientific Heritage part1
Indian Scientific Heritage part1Indian Scientific Heritage part1
Indian Scientific Heritage part1
 
The Arm Group 09
The Arm Group 09The Arm Group 09
The Arm Group 09
 
Html css
Html cssHtml css
Html css
 
Quick Personality Test
Quick Personality TestQuick Personality Test
Quick Personality Test
 
Mobile On Education
Mobile On EducationMobile On Education
Mobile On Education
 
Total Asset Visibility For Defense
Total Asset Visibility For DefenseTotal Asset Visibility For Defense
Total Asset Visibility For Defense
 
O'Smiley
O'SmileyO'Smiley
O'Smiley
 
Variables & Expressions
Variables & ExpressionsVariables & Expressions
Variables & Expressions
 
Cochin Flower Show 2010
Cochin Flower Show 2010Cochin Flower Show 2010
Cochin Flower Show 2010
 
Expocomm Mario Massone
Expocomm Mario MassoneExpocomm Mario Massone
Expocomm Mario Massone
 
Purrsuit recap2
Purrsuit recap2Purrsuit recap2
Purrsuit recap2
 
TextNoMore Media Deck
TextNoMore Media DeckTextNoMore Media Deck
TextNoMore Media Deck
 
Global Strategy In Our Changing Aerospace Environment Stanford Ibc April 17...
Global Strategy In Our Changing Aerospace Environment   Stanford Ibc April 17...Global Strategy In Our Changing Aerospace Environment   Stanford Ibc April 17...
Global Strategy In Our Changing Aerospace Environment Stanford Ibc April 17...
 
FP2003 ch 3~5
FP2003 ch 3~5FP2003 ch 3~5
FP2003 ch 3~5
 
Week7
Week7Week7
Week7
 
BleachBright Intro
BleachBright IntroBleachBright Intro
BleachBright Intro
 
Mdi Displays
Mdi DisplaysMdi Displays
Mdi Displays
 
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
 

Similar to Conditionals, basic list manipulation and pattern matching

Intro python
Intro pythonIntro python
Intro python
kamzilla
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
mrkurt
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 

Similar to Conditionals, basic list manipulation and pattern matching (20)

Iteration
IterationIteration
Iteration
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 
Oo Perl
Oo PerlOo Perl
Oo Perl
 
Intro python
Intro pythonIntro python
Intro python
 
Groovy every day
Groovy every dayGroovy every day
Groovy every day
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Php 2
Php 2Php 2
Php 2
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Php Basic
Php BasicPhp Basic
Php Basic
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Perls Functional functions
Perls Functional functionsPerls Functional functions
Perls Functional functions
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 

Recently uploaded

Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
rajveermohali2022
 
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
rajveermohali2022
 
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
Apsara Of India
 
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
rajveermohali2022
 
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
Apsara Of India
 
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts ServiceVIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
Apsara Of India
 
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
Sheetaleventcompany
 
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
Sheetaleventcompany
 
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
Sheetaleventcompany
 
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
Apsara Of India
 
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call GirlsVIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
Apsara Of India
 

Recently uploaded (20)

Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
 
Sakinaka Call Girls Agency 📞 9892124323 ✅ Call Girl in Sakinaka
Sakinaka Call Girls Agency  📞 9892124323 ✅  Call Girl in SakinakaSakinaka Call Girls Agency  📞 9892124323 ✅  Call Girl in Sakinaka
Sakinaka Call Girls Agency 📞 9892124323 ✅ Call Girl in Sakinaka
 
Nahan call girls 📞 8617697112 At Low Cost Cash Payment Booking
Nahan call girls 📞 8617697112 At Low Cost Cash Payment BookingNahan call girls 📞 8617697112 At Low Cost Cash Payment Booking
Nahan call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
Chandigarh Escorts Service 📞9915851334📞 Just📲 Call Rajveer Chandigarh Call Gi...
 
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
 
Just Call Vip call girls Kolhapur Escorts ☎️8617370543 Starting From 5K to 25...
Just Call Vip call girls Kolhapur Escorts ☎️8617370543 Starting From 5K to 25...Just Call Vip call girls Kolhapur Escorts ☎️8617370543 Starting From 5K to 25...
Just Call Vip call girls Kolhapur Escorts ☎️8617370543 Starting From 5K to 25...
 
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
💞5✨ Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
 
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
Chandigarh Escorts Service ☎ 9915851334 ☎ Top Class Call Girls Service In Moh...
 
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
 
Call Girls Dholpur Just Call 8617370543Top Class Call Girl Service Available
Call Girls Dholpur Just Call 8617370543Top Class Call Girl Service AvailableCall Girls Dholpur Just Call 8617370543Top Class Call Girl Service Available
Call Girls Dholpur Just Call 8617370543Top Class Call Girl Service Available
 
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts ServiceVIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
VIP Call Girls In Karnal 08168329307 Nilokheri Call Girls Escorts Service
 
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
Chandigarh Escort Service 📞9878281761📞 Just📲 Call Navi Chandigarh Call Girls ...
 
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
👉Amritsar Call Girl 👉📞 8725944379 👉📞 Just📲 Call Mack Call Girls Service In Am...
 
Top 10 Moisturising Cream Brands In India - Stelon Biotech
Top 10 Moisturising Cream Brands In India - Stelon BiotechTop 10 Moisturising Cream Brands In India - Stelon Biotech
Top 10 Moisturising Cream Brands In India - Stelon Biotech
 
High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱
 
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
👉Amritsar Escorts📞Book Now📞👉 8725944379 👉Amritsar Escort Service No Advance C...
 
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
 
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call GirlsVIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
VIP 💞🌷Call Girls In Karnal 08168329307 Escorts Service Nilokheri Call Girls
 
"Maximizing your savings:The power of financial planning".pptx
"Maximizing your savings:The power of financial planning".pptx"Maximizing your savings:The power of financial planning".pptx
"Maximizing your savings:The power of financial planning".pptx
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
 

Conditionals, basic list manipulation and pattern matching

  • 1. AI Programming Week Four: Conditionals, Lists and Pattern Matching Richard Price rmp@ cs.bham.ac.uk www.cs.bham.ac.uk/~rmp/ www.cs.bham.ac.uk/internal/courses/ai-prog-a/
  • 2. C o nditio na ls • If <this statement is true> then … if <condition> then <code> endif; lvars myVariable = 1 0; if myVariable = 1 0 then ‘My Variable is equal to 1 0’ => endif; 2
  • 3. C o m pa ris o n O pera to rs • = ( equals) • < ( Less than) • > ( Greater than) • <= ( Less than or equal to) • >= ( Greater than or equal to) lvars myVariable = 1 0; myVariable = 1 0 => * <true> * 3
  • 4. I f … a nd/or … if <condition> and <condition> then <code> endif; lvars myVariable = 1 0; if myVariable < 1 1 and myVariable > 9 then ‘My Variable is 1 0! => ’ endif; lvars myPet = ‘duck’ if myPet = ‘duck’ or myPet = ‘budgie’ then ‘My pet is a bird! => ’ endif; 4
  • 5. I f … then … els e … if <condition> then <code> else <code> endif; lvars myPet = ‘cat’; if myPet = ‘duck’ or myPet = ‘budgie’ then ‘My pet is a bird! => ’ Else ‘My pet is not a bird :( => ’ endif; 5
  • 6. I f … then … els eif … els e … lvars myVariable = 1 1 ; if myVariable < 1 1 ‘My Variable is less than 1 1 ! => ’ elseif myVariable > 1 1 ‘My Variable is greater than 1 1 ! => ’ else ‘My Variable is 1 1 ! => ’ endif; 6
  • 7. I f … then … els eif … els e … define coffeeBreak( bored, money)- response; > if bored = ‘true’ then if money >= 5 then ‘Hurrah!Coffee and cake! - response; ’> elseif money > 2 ‘Coffee! - response; ’> else ‘Oh noes… water… ’ - response; > endif; else ‘W ork, work’ - response; > endif; enddefine; coffeebreak( ‘true’, 5)=> 7
  • 8. L is ts • Lists hold pieces of data – Of any type. – In sequence. lvars myList [a b c]; myList => * [a b c] * myList( => 2) *b * 8
  • 9. L is ts in lis ts • Nested lists like: lvars myNestedList = [a b [c d] [e f]]; • Can be printed out using the ‘pretty print arrow’: myNestedList ==> * [a b * [c d] [e f]] 9
  • 10. A dding to L is ts • The operators: –^ ( hat) – ^^ ( double hat) lvars myList = [I have a pet], insert = ‘dog’; [I have a pet ^insert] => * I have a pet dog. * 10
  • 11. ^ o r ^^ lvars insert = [and another list]; [I have a list ^insert] => * [I have a list [and another list] ] * [I have a list ^^insert] => * [I have a list and another list] * • ^^ loses the square brackets [ ]. • Experiment with this! 11
  • 12. L ink ed L is ts • Pop- 1 uses linked lists. 1 • Sequence of connected nodes. • Each node consists of: – A pointer. – And the data item. – Lists start with a header – End with a footer. – End of list’s pointer is null. 12
  • 13. L ink ed L is ts • Variables simply point to the head of the list. 13
  • 14. L ink ed L is ts • Copying lists copydata( myList)- myOtherList; > 14
  • 15. C ha ng ing a nd A dding L is ts 15
  • 16. L is t func tio ns • Teach … – >< ( concatenation) – :: ( head insertion) – length() – hd( ) – tl() – rev( ) – shuffle() – oneof( ) – sort() 16
  • 17. M a tc hes • Pop- 1 allows us to flexibly match lists. 1 [a b c d] matches [a b c d] => * <true> * • Matches is an operator returning <true> or <false> 17
  • 18. P a ttern M a tc hing • Like ^ and ^^ we can use = and == on lists. • = matches a single element in a list. [a b c d] matches [= b c d] => * <true> * • == matches 0 or more elements. [a b c d] matches [== b c d] => [a b c d] matches [a == d] => [a b c d] matches [a b c ==] => [a b c d] matches [a b c d ==] => 18
  • 19. P a ttern M a tc hing • And combine them: [a b c d] matches [= b ==] => * <true> * • What about? [a b c d] matches [= b = =] => [a b c d] matches [==] => [a b c d] matches [= = = =] => [a b c d] matches [== == = = = = ==] => [a b c d] matches [a == c = =] => 19
  • 20. P a ttern M a tc hing • Like ^, ^^, = and == we can use ? and ??: [a b c d] matches [= b ?anItem ==]; anItem => *c * [a b c d] matches [a b ??items]; Items => * [c d] * 20
  • 21. P a ttern M a tc hing Lvars input, name; ‘Hello what is your name? => Readline( - input; ;;; reads a line from the keyboard. )> If input matches ! [Hello my name is ?name] then [Hello ^name] => Else [Erm… hello] => Endif; 21