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

Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
rajveermohali2022
 
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
Nitya salvi
 
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
Sheetaleventcompany
 
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
rajveerescorts2022
 
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
Sheetaleventcompany
 

Recently uploaded (20)

Payal Mehta 9867746289, Escorts Service Near The Taj Mahal Palace Colaba
Payal Mehta 9867746289, Escorts Service Near The Taj Mahal Palace ColabaPayal Mehta 9867746289, Escorts Service Near The Taj Mahal Palace Colaba
Payal Mehta 9867746289, Escorts Service Near The Taj Mahal Palace Colaba
 
Tirunelveli Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tirunelveli
Tirunelveli Escorts Service Girl ^ 9332606886, WhatsApp Anytime TirunelveliTirunelveli Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tirunelveli
Tirunelveli Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tirunelveli
 
Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
Kharar Call Girls Service✔️ 9915851334 ✔️Call Now Ranveer📲 Zirakpur Escort Se...
 
Call Girls In Mohali ☎ 9915851334☎ Just Genuine Call Call Girls Mohali 🧿Elite...
Call Girls In Mohali ☎ 9915851334☎ Just Genuine Call Call Girls Mohali 🧿Elite...Call Girls In Mohali ☎ 9915851334☎ Just Genuine Call Call Girls Mohali 🧿Elite...
Call Girls In Mohali ☎ 9915851334☎ Just Genuine Call Call Girls Mohali 🧿Elite...
 
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
Call Girls In Raigad Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service Enjoy...
 
9867746289, Unveiling the Secrets of Russian Escort Services in Mumbai, Vashi...
9867746289, Unveiling the Secrets of Russian Escort Services in Mumbai, Vashi...9867746289, Unveiling the Secrets of Russian Escort Services in Mumbai, Vashi...
9867746289, Unveiling the Secrets of Russian Escort Services in Mumbai, Vashi...
 
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East P...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East P...Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East P...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East P...
 
Escorts Service Model Basti 👉 Just CALL ME: 8617697112 💋 Call Out Call Both W...
Escorts Service Model Basti 👉 Just CALL ME: 8617697112 💋 Call Out Call Both W...Escorts Service Model Basti 👉 Just CALL ME: 8617697112 💋 Call Out Call Both W...
Escorts Service Model Basti 👉 Just CALL ME: 8617697112 💋 Call Out Call Both W...
 
gatiin-namaa-meeqa .pdf
gatiin-namaa-meeqa                        .pdfgatiin-namaa-meeqa                        .pdf
gatiin-namaa-meeqa .pdf
 
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
💚Call Girl In Amritsar 💯Anvi 📲🔝8725944379🔝Amritsar Call Girls No💰Advance Cash...
 
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
Zirakpur Call Girls ✅ Just Call ☎ 9878799926☎ Call Girls Service In Mohali Av...
 
❤️Amritsar Call Girls☎️9815674956☎️ Call Girl service in Amritsar☎️ Amritsar ...
❤️Amritsar Call Girls☎️9815674956☎️ Call Girl service in Amritsar☎️ Amritsar ...❤️Amritsar Call Girls☎️9815674956☎️ Call Girl service in Amritsar☎️ Amritsar ...
❤️Amritsar Call Girls☎️9815674956☎️ Call Girl service in Amritsar☎️ Amritsar ...
 
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
Call Girls Service In Zirakpur ❤️🍑 7837612180 👄🫦Independent Escort Service Zi...
 
Tumkur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tumkur
Tumkur Escorts Service Girl ^ 9332606886, WhatsApp Anytime TumkurTumkur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tumkur
Tumkur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tumkur
 
Mahim Call Girls in Bandra 7738631006, Sakinaka Call Girls agency, Kurla Call...
Mahim Call Girls in Bandra 7738631006, Sakinaka Call Girls agency, Kurla Call...Mahim Call Girls in Bandra 7738631006, Sakinaka Call Girls agency, Kurla Call...
Mahim Call Girls in Bandra 7738631006, Sakinaka Call Girls agency, Kurla Call...
 
Just Call Vip call girls Etawah Escorts ☎️8617370543 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️8617370543 Two shot with one girl (...Just Call Vip call girls Etawah Escorts ☎️8617370543 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️8617370543 Two shot with one girl (...
 
Yamunanagar Escorts Service Girl ^ 9332606886, WhatsApp Anytime Yamunanagar
Yamunanagar Escorts Service Girl ^ 9332606886, WhatsApp Anytime YamunanagarYamunanagar Escorts Service Girl ^ 9332606886, WhatsApp Anytime Yamunanagar
Yamunanagar Escorts Service Girl ^ 9332606886, WhatsApp Anytime Yamunanagar
 
Tinted Sunscreen For Soft and Smooth Skin
Tinted Sunscreen For Soft and Smooth SkinTinted Sunscreen For Soft and Smooth Skin
Tinted Sunscreen For Soft and Smooth Skin
 
UNIVERSAL HUMAN VALUES - INTRODUCTION TO VALUE EDUCATION
 UNIVERSAL HUMAN VALUES - INTRODUCTION TO VALUE EDUCATION UNIVERSAL HUMAN VALUES - INTRODUCTION TO VALUE EDUCATION
UNIVERSAL HUMAN VALUES - INTRODUCTION TO VALUE EDUCATION
 
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
 

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