Commit e31cafea authored by Frank Huch's avatar Frank Huch

Add code from lecture

parent d6ba029e
mother(susanne,monika).
mother(norbert,monika).
mother(monika,ulrike).
mother(herbert,christine).
mother(andreas,angelika).
mother(angelika,christine).
mother(hubert,maria).
female(susanne).
female(P) :- mother(_,P).
husband(monika,herbert).
husband(angelika,hubert).
husband(christine,heinz).
husband(maria,fritz).
father(C,F) :- mother(C,M), husband(M,F).
grandmother(C,GM) :- mother(C,M), mother(M,GM).
grandmother(C,GM) :- father(C,F), mother(F,GM).
sibling(P1,P2) :- mother(P1,M), mother(P2,M), P1\=P2.
sister(P,S) :- sibling(P,S), female(S).
date(date(_,_,_)).
year(date(_,_,Y),Y).
month(date(_,M,_),M).
day(date(D,_,_),D).
% data Tree where
% Leaf :: Int -> Tree
% Branch :: Tree -> Tree -> Tree
tree(leaf(_)).
tree(branch(TL,TR)) :- tree(TL),tree(TR).
list([]).
list([_|Xs]) :- list(Xs).
memb(E,[E|_]).
memb(E,[_|Xs]) :- memb(E,Xs).
% (++) :: [a] -> [a] -> [a]
% [] ++ ys = ys
% (x:xs) ++ ys = x : (xs ++ ys)
% app([a],[a],[a])
% the result of a function gets an additional argument
% (usually in the end).
app([] ,Ys,Ys).
app([X|Xs],Ys,[X|Zs]) :- app(Xs,Ys,Zs).
prefix(Xs,Ys) :- app(Xs,_,Ys).
suffix(Xs,Ys) :- app(_,Xs,Ys).
sublist(Xs,Ys) :- prefix(Zs,Ys), suffix(Xs,Zs).
mem(E,Xs) :- sublist([E],Xs).
isPeano(o).
isPeano(s(N)) :- isPeano(N).
add(o ,M,M).
add(s(N),M,s(R)) :- add(N,M,R).
sub(N,M,R) :- add(M,R,N).
leq(o,_).
leq(s(N),s(M)) :- leq(N,M).
len([],o).
len([_|Xs],s(R)) :- len(Xs,R).
% programmin pattern: generate and test
% generator
perm([],[]).
perm([X|Xs],Ys) :- perm(Xs,Zs), insert(X,Zs,Ys).
insert(X,Ys ,[X|Ys]).
insert(X,[Y|Ys],[Y|Zs]) :- insert(X,Ys,Zs).
% test
sorted([]).
sorted([_]).
%sorted([X1,X2|Xs]) :- leq(X1,X2), sorted([X2|Xs]).
sorted([X1,X2|Xs]) :- X1=<X2, sorted([X2|Xs]).
% solve the problem
sort(Xs,Ys) :- perm(Xs,Ys), % generate possible solutions
sorted(Ys). % test solution on correctness
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment