Commit 1f97991c by Max Rapp

### Merge branch 'master' of gl.kwarc.info:teaching/AI

parents f3f4d893 d5d5c12c
 Readme === AI 1 - 2019/2020 --- * 2019-10-28: `prolog/2019-10-28-tutorial.pl` (simple prolog exercises) * 2019-10-31 (substition for Katja): `prolog/2019-10-31-tutorial.pl` (simple prolog exercises) * 2019-11-04: `prolog/2019-11-04-trees.pl` (some simple tree exercises) * 2019-11-11: `2019-11-11-nary-trees.pl` (tree exercise in preparation for tree-search homework)
 % This is the prolog file we created during the tutorial. % % Remark: inspired by this problem list: http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/ % INTRODUCTION country(germany). geolocation(X) :- country(X). bigger(russia, germany). bigger(germany, belgium). bigger(A, B) :- bigger(A, C), bigger(C, B). % LIST EXERCISES firstelement([A|_], A). lastelement([A], A). lastelement([_|A], B) :- lastelement(A, B). secondlast([H,_], H). secondlast([_|A], B) :- secondlast(A, B). second([], []). second([A], [A]). second([A,_|B], [A|Restlist]) :- second(B, Restlist). prepend(A, B, [A|B]). duplicate([], []). duplicate([A], [A,A]). duplicate([H|T], [H,H|T2]) :- duplicate(T, T2). % INSERTION SORT insert(Element, [], [Element]). insert(Element, [Hs|Ts], [Element,Hs|Ts]):- Element =< Hs. insert(Element, [Hs|Ts], [Hs|Tl]) :- Element >= Hs, insert(Element, Ts, Tl). inssort([], []). inssort([H|T], Result) :- inssort(T, X), insert(H, X, Result).
 % INTRODUCTION animal(elephant). bigger(elephant, bird). bigger(elephant, cow). bigger(cow, mouse). bigger(X, Y) :- bigger(X, A), bigger(A, Y). % Note: The above definition for a transitive relation % results in loop, which wasn't resolved during the tutorial. % If you want to know more about it, take a look at this answer: % https://stackoverflow.com/a/28615704 % SIMPLE LIST TASKS % inspired from http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/ getfirst([X|_], X). getlast([X], X). getlast([_|R], X) :- getlast(R, X). getsecondlast([X,_], X). getsecondlast([_|R], X) :- getsecondlast(R, X). getlength([], 0). getlength([X|R], L) :- getlength(R, N), L is N+1. second([], []). second([X], [X]). second([X,Y|R], [X|S]) :- second(R, S). fourth(X, Y) :- second(X, Z), second(Z, Y). prepend(X,SomeList,[X|SomeList]). duplicate([], []). duplicate([X|R], [X,X|S]) :- duplicate(R, S). % INSERTION SORT insert(X, [], [X]). insert(X, [H|R], [H|S]) :- X >= H, insert(X, R, S). insert(X, [H|R], [X,H|R]) :- X =< H. inssort([], []). inssort([H|T], L) :- inssort(T, TSorted), write('inserting '), write(H), write('\n'), insert(H, TSorted, L).
 % Check that a tree is indeed a binary tree % (as specified in the homework) isbinary(nil). isbinary(tree(N, X, Y)) :- integer(N), isbinary(X), isbinary(Y). % Helper predicate: get the maximum of two integers % e.g max(2,7,X) results in X=7. % % ! is the cut operator. % It tells prolog not to backtrack anymore. % In this case, we want to ignore the second case, % if the first case is applicable. max(X,Y,X) :- X >= Y, !. max(_,Y,Y). % Get the largest node in a tree % (assuming it only contains natural numbers) treemax(nil, 0). treemax(tree(N, X, Y), Result) :- treemax(X, XRes), treemax(Y, YRes), max(XRes, YRes, XYRes), max(XYRes, N, Result). % Get the depth of a tree treedepth(nil, 0). treedepth(tree(_, X, Y), Depth) :- treedepth(X, XDepth), treedepth(Y, YDepth), max(XDepth, YDepth, D), Depth is D + 1. % helper predicate: concatenate two lists concatenate([], L, L). concatenate([H|T], L, [H|TL]) :- concatenate(T, L, TL). % get the leaves of a binary tree leaves(nil, []). leaves(tree(N, nil, nil), [N]) :- !. leaves(tree(N, X, Y), Result) :- leaves(X, XL), leaves(Y, YL), concatenate(XL, YL, Result). % compute the result of a formula (which you can think of as a tree). % Example tree: plus(times(2,6), 7) compute(SomeInt, SomeInt) :- integer(SomeInt). compute(times(X, Y), R) :- compute(X, XRes), compute(Y, YRes), R is XRes * YRes. compute(plus(X, Y), R) :- compute(X, XRes), compute(Y, YRes), R is XRes + YRes.