numar(1). numar(5). numar(6). numar(20). % 1. % ========================================== % mihai --- wendy % | % ana radu --- miruna % | % ioana alin % parinte(X, Y) -- părintele lui X este Y % Y este parintele lui X % X este copilul lui Y parinte(ioana, radu). parinte(ioana, miruna). parinte(alin, radu). parinte(alin, miruna). parinte(ana, mihai). parinte(ana, wendy). parinte(radu, mihai). parinte(radu, wendy). % vreau sa obtin: [(sotA, sotB), (sotC, sotD)] %soti(A, B) :- parinte(X, A), parinte(X, B), A \= B. sotiA(A, B) :- parinte(X, A), parinte(X, B), A @< B. listaSoti(L) :- findall((A, B), sotiA(A, B), L). listaSotiA(L) :- setof((A, B), sotiA(A, B), L). listaSotiB(L) :- bagof((A, B), ( parinte(X, A), parinte(X, B), A @< B ), L). listaSotiC(L) :- setof((A, B), ( parinte(X, A), parinte(X, B), A @< B ), L). listaSotiD(L) :- setof((A, B), X^( parinte(X, A), parinte(X, B), A @< B ), L). % 2. Cheryl's Birthday % ==================================================== % Albert and Bernard have just met Cheryl. 'When is your birthday?' % Albert asked Cheryl. Cheryl thought for a moment and said, 'I won't % tell you, but I'll give you some clues'. She wrote down a list of ten % dates: % May 15, May 16, May 19 % June 17, June 18 % July 14, July 16 % August 14, August 15, August 17 % 'One of these is my birthday,' she said. % Cheryl whispered in Albert's ear the month, and only the month, of her % birthday. To Bernard, she whispered the day, and only the day. 'Can % you figure it out now?' she asked Albert. % Albert: 'I don't know when your birthday is, but I know Bernard % doesn't know, either.' % Bernard: 'I didn't know originally, but now I do.' % Albert: 'Well, now I know, too!' % When is Cheryl's birthday? dates([may/15, may/16, may/19, june/17, june/18, july/14, july/16, aug/14, aug/15, aug/17]). % statement 1a: Albert: I don't know % in luna pe care o stie Albert (in luna care este solutie a problemei) % sunt mai multe date in lista de date s1a(Month/Day) :- dates(DS), member(Month/Day, DS), % findall(D, member(Month/D, DS), L), length(L, LenL), LenL >= 2. findall(D, member(Month/D, DS), [_, _|_]). % \+ findall(D, member(Month/D, DS), [_]). % statement 1b: Albert: Bernard doesn't know s1bA(Month/Day) :- dates(DS), member(Month/Day, DS), s1a(Month/Day), % in luna pe care o stie Albert, nu este nicio zi care % nu am nicio zi D in luna Month pentru care este adevarat % ca implica unic data (implica unic o luna M) findall(D, (member(Month/D, DS), % am o unica solutie care contine ziua D findall(M/D, member(M/D, DS), [_]) ), []). % nicio zi D s1bB(Month/Day) :- dates(DS), member(Month/Day, DS), s1a(Month/Day), % oricare zi in luna pe care o stie Albert, este fals ca implica unic data % pentru oricare zi D din luna Month forall(member(Month/D, DS), % este fals ca D implica unic o luna M \+ findall(M/D, member(M/D, DS), [_])). % statement 2: Bernard: I know s2(Month/Day) :- s1bA(Month/Day), % exista o singura luna M pentru care discutia anterioara % este adevarata, data fiind ziua Day findall(M, s1bA(M/Day), [_]). % statement 3: Albert: I know s3(Month/Day) :- s2(Month/Day), % exista o singura data D pentru care discutia anterioara este % adevarata, data fiind luna Month findall(D, s2(Month/D), [_]).