324-lecture7 - Principles Principles of Programming...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Principles of Programming Languages Lecture 7 Wael Aboulsaadat [email protected] http://portal.utoronto.ca/ 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Prolog: structures Prolog: structures – example 2 example 2 Family database – cont’d: amily( erson(tom,fox,date(7,may,1950),works(cbc,15200)), erson(ann,fox,date(9,may,1951), works(ctv,25700)), person(pat,fox,date(5,may,1973),unemployed), person(jim,fox,date(5,may,1973),unemployed)]). Let us add more useful rules usband(X) :- family(X,_,_). % Find the names of all the people in database | ?- exists(person(Name,Surname,_,_)). Name = tom Surname = fox; Name = ann Surname = fox; Name = pat Surname = fox; ife(X) :- family(_,X,_). hild(X) :- family(_,_,Children), member(X,Children). xists(Person) :-husband(Person); wife(Person);child(Person). alary(person(_,_,_,works(_,S)),S). alary(person(_,_,_,unemployed),0). ateofbirth(person(_,_,Date,_),Date). Name = jim Surname = fox; no % Find all children born in 1973 | ?- child(X),dateofbirth(X,date(_,_,1973)). X = person(pat,fox,date(5,may,1973), unemployed); X = person(jim,fox,date(5,may,1973), unemployed); no 2
Background image of page 2
Prolog: complex types Prolog: complex types - structures structures Recall: what’s a function term? functor (some-parameters) e.g. woman(marry) We can construct complex data structures using nested function terms and lists. – Represents a statement about the world Example 2: – A family consist of 2 persons, and 0 or more children. Each person is either employed for some salary or unemployed. family(person(First-name,Last-name,date(Day,Month,Year),works(Company,Salary)), person(First-name,Last-name,date(Day,Month,Year),works(Company,Salary)), [person(First-name,Last-name,date(Day,Month,Year),unemployed), person(First-name,Last-name,date(Day,Month,Year),unemployed)]). 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Prolog: structures Prolog: structures – example 2 example 2 Family database – cont’d: % To find the names of unemployed people who were born before 1975 | ?- exists(person(Name,Surname,date(_,_,Year),unemployed)),Year < 1975. Name = pat Surname = fox Year = 1973; Name = jim Surname = fox Year = 1973; no % To find people born before 1951 whose salary is less than 80000 | ?- exists(Person),dateofbirth(Person,date(_,_,Year)),Year<1951, salary(Person,Salary), Salary<80000. Person = person(tom,fox,date(7,may,1950),works(cbc,15200)) Year = 1950 Salary = 15200; no 4
Background image of page 4
Prolog: structures Prolog: structures – example 2 example 2 Family database – cont’d: % A rule to add the salaries total([],0). total([Person|Tail],Sum):- salary(Person,S),total(Tail,Rest),Sum is S + Rest. % To find the total income of family | ?- family(Husband,Wife,Children),total([Husband,Wife|Children],Income). Husband = person(tom,fox,date(7,may,1950),works(cbc,15200)) Wife = person(ann,fox,date(9,may,1951),works(ctv,25700)) Children = [person(pat,fox,date(5,may,1973),unemployed), person(jim,fox,date(5,may,1973),unemployed)] Income = 40900; % Now, let us define how to get the nth child. Note: I left children clause for you to define
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

324-lecture7 - Principles Principles of Programming...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online