diff --git a/Frederik/README.md b/Frederik/README.md index 095beb095899cbe9d9c5ce6dcb2308f7baa87d14..4533f32698f321f04ed1498f6bf6d1356756798d 100644 --- a/Frederik/README.md +++ b/Frederik/README.md @@ -4,4 +4,5 @@ Readme AI 1 - 2019/2020 --- -* 2019-10-28: `2019-10-28-tutorial.pl` (simple prolog exercises) +* 2019-10-28: `prolog/2019-10-28-tutorial.pl` (simple prolog exercises) +* 2019-10-31: `prolog/2019-10-31-tutorial.pl` (simple prolog exercises) diff --git a/Frederik/prolog/2019-10-31-tutorial.pl b/Frederik/prolog/2019-10-31-tutorial.pl new file mode 100644 index 0000000000000000000000000000000000000000..11c4f282d0e9b46ca512e11a24b09a47aefa63d4 --- /dev/null +++ b/Frederik/prolog/2019-10-31-tutorial.pl @@ -0,0 +1,65 @@ +% 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). +