Beruflich Dokumente
Kultur Dokumente
position(‘Spielberg’, director).
position(‘Allen’, manager).
position(‘Lee’, supervisor).
find_position:-
?- find_position.
Whose position do you wish to know?
‘Spielberg’.
The position of Spielberg is director
isort([],[]).
isort([X|Tail],SList):-
isort(Tail,STail),
insert(X,STail,SList).
The isort predicate strips off the head of the list, sorts the
tail recursively, then finally inserts the head item (X) into
the correct location.
insert(X,[],[X]).
insert(X,[H|Tail],[X,H|Tail]):-
X =< H.
insert(X,[H|Tail],[H|NewTail]):-
X > H,
insert(X,Tail,NewTail).
The first clause terminates the search process when the item (X)
is to be inserted into an empty list, it creates a new list with a
single item (X) in it.
The final clause matches for the case when the item (X) is
greater than the head of the list (H). In this case the item is
inserted into the current Tail.
?- isort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1], S)
S = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Steps involved:
- Change each you to i
- Change are to am not
- Change french to german
- Change do to no
Predicate alter
?- alter([do you know french], Y].
Y = [no i know german]
Main functions:
• change head of input list into another word, let this
word become head of output list
• alter tail of input list and let it become the tail of output
list
• if we have reached the end of the input list, then
terminate the output list with an empty list [ ]
Defining change
Two cases
1. First argument an empty list
append( [ ], L, L)
[X | L1]
X L1 L2
L3
X L3
[X | L3]
More (y/n)? y
X = [a,b]
Y = [c]
More (y/n)? y
X = [a,b,c]
Y = []
Predicate myinsert
MORE ON RECURSION
peter
(margaret)
trevor nicholas
married(peter,margaret).
married(john,barbara).
child_of(john,peter).
child_of(christine,peter).
child_of(jennifer,peter).
child_of(john,margaret).
child_of(christine,margaret).
child_of(jennifer,margaret).
.
.
etc.
parent_of(X,Y) :- child_of(Y,X).
descendent_of(X,Y) :- child_of(X,Y).
descendent_of(X,Y) :- child_of(X,A) ,
descendent_of(A,Y).
Searching
Examples:
- Find a route to get to the city from Murdoch (the
freeway is closed!)
The doctor does not require tests that are not somehow
indicated by the symptoms at hand