# Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.

## Presentation on theme: "Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic."— Presentation transcript:

Chapter 3: Lists, Operators, Arithmetic Part 1

Outline Representation of lists Some operations in lists Operator notation Arithmetic

Representation of lists

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]

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

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

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, []))))

Representation of lists ann tennis tom [ ] skiing

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

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

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

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

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

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

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]

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]

Download ppt "Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic."

Similar presentations