Download presentation

Presentation is loading. Please wait.

Published byTyrone Avon Modified over 2 years ago

1
Chapter 3: Lists, Operators, Arithmetic Part 1

2
Outline Representation of lists Some operations in lists Operator notation Arithmetic

3
Representation of lists

4
The list is a simple data structure. A list is a sequence of any number of items. e.g. ann, tennis, tom, skiing written in prolog as: [ann, tennis, tom, skiing]

5
Representation of lists How can a list be represented as a standard Prolog object? There are 2 cases: 1.list is empty : written as Prolog atom, []. 2.list is non-empty : it will consist two things: The first item, called Head. The remaining part of list, called Tail

6
Representation of lists For example: [ann, tennis, tom, skiing] The head is : ann The tail is the list : [tennis, tom, skiing] The head can be any Prolog object The tail has to be list

7
Representation of lists The head and the tail can be combined into a structure by a special functor..(Head,Tail) The tail is a list, it is either empty or it has its own head and tail. The previous list can be represented as the term:.(ann,.(tennis,.(tom,.(skiing, []))))

8
Representation of lists ann tennis tom [ ] skiing

9
Representation of lists ?- List1=[a,b,c], List2=.(a,b,c). List1= [a,b,c] List2=[a,b,c] ?- Hobbies1=.(tennis,.(reading,[])), Hobbies2=[skiing,food], L=[ann,Hobbies1,tom,Hobbies2]. Hobbies1=[tennis,reading] Hobbies2=[skiing,food] L=[ann,[tennis,reading],tom,[skiing,food]]

10
Representation of lists The element of a list can be object of any kind; it can be also list e.g. L= [a,b,c] can be written as: and Tail=[b,c] and then L=.(a, Tail) To express it in the square bracket notation we can use vertical bar that separate the head and tail: L=[a|Tail] Alternative ways of writing the list L are: [a,b,c]=[a|[b,c]]=[a,b|[c]]=[a,b,c|[]]

11
Some operations on lists 1.Membership 2.Concatenation 3.Adding an item 4.Deleting an item

12
Some operations on lists member(X,L) where X is an object and L is a list member(X,L) is true if X occurs in L e.g. member(b, [a,b,c]) true member(b, [a,[b,c]]) false member([b,c], [a,[b,c]]) true Definition of member: X is a member of L if either: (1)X is the head of L, or (2)X is a member of the tail of L. member(X,[X|Tail]). member(X,[Head|Tail]):- member(X,Tail).

13
A list is a finite sequence of elements Examples of lists in Prolog: [mia, vincent, jules, yolanda] [mia, robber(honeybunny), X, 2, mia] [ ] [mia, [vincent, jules], [butch, friend(butch)]] [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] summary

14
List elements are enclosed in square brackets The length of a list is the number of elements it has All sorts of Prolog terms can be elements of a list There is a special list: the empty list [ ]

15
summary Head and Tail A non-empty list can be thought of as consisting of two parts The head The tail The head is the first item in the list The tail is everything else The tail is the list that remains when we take the first element away The tail of a list is always a list

16
Class exercise (1) What does Prolog answer the following member(tea,[tea,coffee,juce]). member(milk,[tea,coffee,juce]). Using a vertical bar notation, create a list L with a head a and Tail = [c,d,c]

17
Answers: ?- member(tea,[tea,coffee,juce]). true ; ?- member(milk,[tea,coffee,juce]). false. ?- Tail=[c,d,e], L=[a|Tail]. Tail = [c, d, e], L = [a, c, d, e]

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google