Prolog EN -- Laboratory 4 -- 2006-2007 -- info.uvt.ro

From Wikiversity


Relations[edit]

Example 1[edit]

  • This example was extracted from the site: [1]

The relations database:

has(joe, car(ford, 3, 5000)).
has(joe, car(opel, 2, 6000)).
has(mick, car(toyota, 5, 1000)).
has(mick, car(ford, 2, 2000)).
has(frank, car(reno, 7, 3500)).
has(mike, car(fiat, 4, 2000)).
has(ion, car(dacia, 10, 100)).

The queries:

  • List all the car owners.
  • List all the available cars.
  • List all the cars below a given price.
  • List all the owners that have two or more cars.


Example 2[edit]

  • The contents of the relations was extracted from the Oracle sample database schema. [2]

The relations database:

department(10, accounting, new_york, 7782).
department(20, research, dallas, 7566).
department(30, sales, chicago, 7698).
department(40, operations, new_york, 7839).
employee(7369, smith,  clerk,     7902, 20).
employee(7499, allen,  salesman,  7698, 30).
employee(7521, ward,   salesman,  7698, 30).
employee(7566, jones,  manager,   7839, 20).
employee(7654, martin, salesman,  7968, 30).
employee(7698, blake,  manager,   7839, 30).
employee(7782, clark,  manager,   7839, 10).
employee(7788, scott,  analyst,   7566, 20).
employee(7839, king,   president, *,    10).
employee(7844, turner, salesman,  7698, 30).
employee(7876, adams,  clerk,     7788, 20).
employee(7900, james,  clerk,     7698, 30).
employee(7902, ford,   analyst,   7566, 20).
employee(7934, miller, clerk,     7782, 10).

The queries:

  • List for all the employees the names of their manager.
  • List for all the employees the name of the town in which they work.
  • List for all the employees the name of their department manager.
  • List all the coworker pairs, but excluding the repetitions.
  • List for all the employees the depth in the organizational hierarchy.


Binary sorted trees[edit]

insert(X, *, tree(X, *, *)).
insert(Y, T, NT) :- ...
create(List, Tree) :- ...

Example:

create([1, 2, 3], T). %=> T = tree(3, tree(2, tree(1, *, *), *), *).


Graphs[edit]

  • This example was extracted from the site: [3]
edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).

The queries:

  • node(X)
  • connected(X,Y)
  • nodes(L)
  • path(A,B,Path)


ccraciun@info.uvt.ro