conditionals, basic list manipulation and pattern matching
TRANSCRIPT
www.cs.bham.ac.uk/internal/courses/ai-prog-a/
AI Programming
Richard [email protected]/~rmp/
Week Four:Conditionals, Lists and Pattern Matching
2
C onditiona ls
• If <this statement is true> then …
if <condition> then<code>
endif;
lvars myVariable = 10;if myVariable = 10 then
‘My Variable is equal to 10’ =>endif;
3
C omparis on Operators
• = (equals)• < (Less than)• > (Greater than)• <= (Less than or equal to)• >= (Greater than or equal to)
lvars myVariable = 10;myVariable = 10 =>** <true>
4
I f … and/or …if <condition> and <condition> then
<code>endif;
lvars myVariable = 10;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;
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;
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;
7
I f … then … els eif … els e …define coffeeBreak(bored, money) -> response;
if bored = ‘true’ thenif money >= 5 then
‘Hurrah! Coffee and cake!’ -> response;elseif money > 2
‘Coffee!’ -> response;else
‘Oh noes… water… ’ -> response;endif;
else‘Work, work’ -> response;
endif;enddefine;coffeebreak(‘true’, 5) =>
8
Lis ts
• Lists hold pieces of data– Of any type.– In sequence.
lvars myList [a b c];myList =>** [a b c]
myList(2) =>** b
9
Lis 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]]
10
Adding to Lis ts
• The operators:– (hat)– (double hat)
lvars myList = [I have a pet], insert = ‘dog’;[I have a pet insert] =>
** I have a pet dog.
11
^ or ^^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!
12
Linked Lis ts
• Pop-1 1 uses linked lists.• 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..
13
Linked Lis ts
• Variables simply point to the head of the list.
14
Linked Lis ts
• Copying lists
copydata(myList) -> myOtherList;
15
C hang ing and Adding Lis ts
16
Lis t func tions• Teach …
– >< (concatenation)– :: (head insertion)– length()– hd()– tl()– rev()– shuffle()– oneof()– sort()
17
M atc hes
• Pop-1 1 allows us to flexibly match lists.
[a b c d] matches [a b c d] =>** <true>
• Matches is an operator returning <true> or <false>
18
Pattern M atc 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 ==] =>
19
Pattern M atc 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 = =] =>
20
Pattern M atc 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]
21
Pattern M atc 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;