Docsity

Prolog Solutions for Artificial Intelligence, Università La Sapienza, Daniele Nardi, Esercizi di Tecniche Di Intelligenza Artificiale

Anteprima parziale del testo

Scarica Prolog Solutions for Artificial Intelligence, Università La Sapienza, Daniele Nardi e più Esercizi in PDF di Tecniche Di Intelligenza Artificiale solo su Docsity!

Artificial Intelligence - Prolog Solutions

useful notes for your final exam

Michele Ciciolla

Master in Artificial Intelligence and Robotics

Sapienza University

Rome

Winter 2018

Contents

  • 1 find depth of node containig value = x.txt
  • 2 binary trees.txt
  • 3 lists.txt
  • 4 lists of lists.txt

2 binary trees.txt

% BINARY TREES -

% T = t(2, t(3,nil,nil), t(4,nil,nil) ),

% ------------------------- ex --------------------------------------- % CONTA FOGLIE DI UN binary tree

conta(nil,0). conta(t(,nil,nil),1):- !. conta(t(,L,R),List):- conta(L,LL), conta(R,RL), append(LL,RL,List).

% ------------------------- ex --------------------------------------- % COLLEZIONA FOGLIE DI UN binary tree in una lista

collectLeaves( nil,[]).

collectLeaves( t(X,nil,nil),[X]):- !.

collectLeaves( t(,L,nil), C):- L = t(,,), collectLeaves(L,C).

collectLeaves( t(,nil,R), C):- R = t(,,), collectLeaves(R,C).

collectLeaves( t(,L,R), C):- L = t(,,), R = t(,,_), collectLeaves(L,C1), collectLeaves(R,C2), append(C1,C2,C).

% qui puoi calcolare il numero delle foglie % length(C,Q). devi cambiare per`o la firma

% ------------------------- ex --------------------------------------- % COLLEZIONA FOGLIE DI UN binary ad una CERTA DEPTH in una lista

collect_depth(nil,,[]). collect_depth(t(X,,_),1,[X]).

collect_depth( t(_,L,R), D, C):- D > 1, D1 is D -1, collect_depth(L,D1,CL), collect_depth(R,D1,CR), append(CL,CR,C).

% ------------------------- ex --------------------------------------- % given a binary tree it return maximum value stored

maximum_tree(nil,0). maximum_tree(t(X,nil,nil),X):- !.

maximum_tree(t(X, L, R),M):-

maximum_tree(L,ML), maximum_tree(R,RL), M is max(RL,ML).

% ------------------------- ex --------------------------------------- % given a binary tree return if X in a member

memberT(nil,). memberT(t(X,,),X):- !. memberT(t(,L,R),X):- memberT(L,X), memberT(R,X).

% ------------------------- ex ---------------------------------------

D=CD,

findD(t(_,L,R), X, CD, D):- CDs is CD+1, findD(L,X,CDs,D), findD(R,X,CDs,D).

depth_of_X(nil,_,0). depth_of_X(Tree,X,Output):- findD(Tree, X, 1, Output).

% call: depth_of_X(t(2, t(3,nil,nil), t(4,nil,nil) ), 4, X). % X = 2

3 lists.txt

% LISTS -

Lista1 = [3,2,1].

joint_3_lists(S1,S2,S3) :- append(S1,S2,L12), write(append(L12,S3,L)).

cancella( X, [X| Tail], Tail). cancella( X, [Y| Tail], [Y|Tail1] ) :- cancella( X, Tail, Tail1).

inserisci(X,L,[X|L]). inserisci(X, [Y|L], [Y|L1]):- inserisci(X,L,L1).

% ------------------------- ex --------------------------------------- % lunghezza di una lista lunghezza(L,N)

lunghezza([],0). lunghezza([_|L],N):- lunghezza(L,N0), N is N0 + 1.

% ------------------------- ex --------------------------------------- % append1(X,Y,Z) is true when Z is concat of X and Y

append1([],Ys,Ys). % caso base append1( [X|Xs], Ys, [X|Zs] ) :- append1(Xs,Ys,Zs).

% ------------------------- ex --------------------------------------- % yes if a list is ordered

4 lists of lists.txt

% LISTS of LISTS - %-----------------------------------

ListaLista1 = [ [a,b,c] , [b,n,r] , [3,2,1] ].

% ------------------------- ex --------------------------------------- % cont number of element of a LoL

conta([],0). conta([Head|Tail],N):- conta(Head,N1), conta(Tail,N2), N is N1 + N2. conta(_X,1).

% ------------------------- ex --------------------------------------- % data una lista di liste di interi ed un intero N, % ritorna una lista i cui elementi sono tutti maggiori di N

greaterN([],_,[]). greaterN( [Head|Tail], N, [Head1|Tail1]):- Head >= N, % Head1 is Head, Head1=Head,

greaterN(Tail, N, Tail1).

greaterN( [Head|Tail], N, [Head1|Tail1]):- Head < N, greaterN(Tail, N, Tail1).

major([],_,[]). major([H|T],N,O):-

greaterN(H,N,O1), major(T,N,O2), % problema: viene chiamata su una lista intera. append(O1,O2,O).

% ------------------------- ex --------------------------------------- % PROGRAMMA che torna gli elementi comuni delle liste di una LOL

% Lol = [ [ 2,3,4],[5,6,2],[4] ], % share(Lol,X). % output X = [2,4]

% share([ [ 2,3,4],[2,5,6],[4] ],X).

add([],_,[]). add( [[Head|Rest]|Tail], E, [Ho1|To1]):-

Head==E, % se gli elementi sono uguali aggiungo H a O Ho1 = Head, add([Rest|Tail],E,To1).

add( [[Head|Rest]|Tail], E, [Ho1|To1]):-

Head ==E , add([Rest|Tail],E,To1).

share([],[]). share([ [H|Head] | Tail ],O):-

add( Tail,H,O1), share(([ Head | Tail ],O2)), append(O1,O2,O).

% ------------------------- ex --------------------------------------- % esonero 2018 - write a prolog program checking for associative list % A = [ [a,f(0)], [b,f(1)], [c,f(2)] ].

% CASO NEGATIVO

findT([Head|Rest] , E, T):-

checkAL([ Head|Rest] ), findT(Rest , E, T).

findE([H|R],E,T):-

% se H=E H==E,

% ritorna T=R T=R.

% ------------------------- ex --------------------------------------- % esame 02/ % compose(INDEXES,VALUES,OUTPUT) % compose([2,1,4,3], [2,4,6,8], X). % output should be X = [4,2,8,6]

% get riceve in input una lista L, un indice N, ritorna in output % l’ elemento E, se presente. Altrimenti nil % get(L,N,E)

get([], _, []). get(L,N,E):-

% N deve essere un numero

accget(L,1,N,E).

accget([H|_],I,N,[He]):-

% usa un accumulatore I per capire a che ciclo store % il caso positivo `e che sono arrivato a I=N,quindi E=H

0 is N-I, He = H. % lo metti in testa alla lista

accget([H|T],I,N,[He|E]):-

% caso negativo, devo andare avanti ed incrementare I

U is I+1, accget(T,U,N,[He|E]).

accget([],,,nil).

compose([],,[]). compose(,[],[]).

compose([Ih| It] ,VALUES ,OUTPUT):-

% prima vai a legge H da Index % poi richiami get(VALUES,H, [E]) % richiamo compose su Values.Tail % fai append tra le due liste

get(VALUES, Ih, O1), compose(It,VALUES,O2), append(O1,O2,OUTPUT).

% ------------------------- ex --------------------------------------- % esame 12/ % GIVEN a LoL of integer and number N returns a LoL containing only those % lists with less than N elems. % lessN([ [1,2,3], [1,5], [4,6] ], 3 , X). -> X = [ [1,5],[4,6] ]

% first i start implementing checklenght(List,N). checklenght([],_).

findL(_,[],[]). findL(E,[E|T],[E]):- !. findL(E,[H|T],O):- findL(E,T,O).

% call: commonL([2,1,4,3], [2,4,6,8], X). % X = [2,4]