21
www.cs.bham.ac.uk/internal/courses/ai-prog-a/ AI Programming Richard Price [email protected] www.cs.bham.ac.uk/~rmp/ Week Four: Conditionals, Lists and Pattern Matching

Conditionals, basic list manipulation and pattern matching

Embed Size (px)

Citation preview

Page 1: Conditionals, basic list manipulation and pattern matching

www.cs.bham.ac.uk/internal/courses/ai-prog-a/

AI Programming

Richard [email protected]/~rmp/

Week Four:Conditionals, Lists and Pattern Matching

Page 2: Conditionals, basic list manipulation 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;

Page 3: Conditionals, basic list manipulation and pattern matching

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>

Page 4: Conditionals, basic list manipulation and pattern matching

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;

Page 5: Conditionals, basic list manipulation and pattern matching

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;

Page 6: Conditionals, basic list manipulation and pattern matching

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;

Page 7: Conditionals, basic list manipulation and pattern matching

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

Page 8: Conditionals, basic list manipulation and pattern matching

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

Page 9: Conditionals, basic list manipulation and pattern matching

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

Page 10: Conditionals, basic list manipulation and pattern matching

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.

Page 11: Conditionals, basic list manipulation and pattern matching

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!

Page 12: Conditionals, basic list manipulation and pattern matching

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

Page 13: Conditionals, basic list manipulation and pattern matching

13

Linked Lis ts

• Variables simply point to the head of the list.

Page 14: Conditionals, basic list manipulation and pattern matching

14

Linked Lis ts

• Copying lists

copydata(myList) -> myOtherList;

Page 15: Conditionals, basic list manipulation and pattern matching

15

C hang ing and Adding Lis ts

Page 16: Conditionals, basic list manipulation and pattern matching

16

Lis t func tions• Teach …

– >< (concatenation)– :: (head insertion)– length()– hd()– tl()– rev()– shuffle()– oneof()– sort()

Page 17: Conditionals, basic list manipulation and pattern matching

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>

Page 18: Conditionals, basic list manipulation and pattern matching

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

Page 19: Conditionals, basic list manipulation and pattern matching

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

Page 20: Conditionals, basic list manipulation and pattern matching

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]

Page 21: Conditionals, basic list manipulation and pattern matching

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;