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
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