% interval % ========================================== % construim bazându-ne pe valori pentru care predicatul a fost deja % demonstrat interval(A, A, _). interval(X, A, B) :- interval(Y, A, B), % merge la infinit după X = 10 X is Y + 1, X > A, X =< B. % evităm ca apelul recursiv să fie prima condiție interval2(X, A, B) :- geninterval(X, A, B, A). geninterval(IT, _, _, IT). geninterval(X, A, B, IT) :- IT1 is IT + 1, IT1 =< B, % mergem ]n recursivitate cu un iterator instantiat geninterval(X, A, B, IT1). % backtracking % ========================================== stateG(S) :- member(S, [a,b,c,d,e,f,g]). nextG(a, b). nextG(a, e). nextG(b, c). nextG(b, d). nextG(d, a). % recursive nextG(e, f). nextG(e, g). initialG(a). finalG(f). finalG(g). problemG(problem(initialG, nextG, finalG)). % declarativ % pe_cale_solutie(S, Path) % S este pe calea dintre initial si final % cu calea Path de la S la solutie pe_cale_solutie(S, Path) :- finalG(S), Path = [S]. pe_cale_solutie(S, Path) :- nextG(S, S1), pe_cale_solutie(S1, PathS1), Path = [S | PathS1]. pe_cale_solutie2(S, Vizitat, Path) :- writeln(Vizitat), finalG(S), reverse(Vizitat, Path). pe_cale_solutie2(S, Vizitat, Path) :- nextG(S, S1), \+ member(S1, Vizitat), pe_cale_solutie2(S1, [S1 | Vizitat], Path). % imperativa % am ajuns la capătul căutării dacă suntem în stare finală search(S, Vizitat, Path) :- finalG(S), reverse(Vizitat, Path). % altfel mergem la o stare următoare care nu este deja vizitată search(S, Vizitat, Path) :- nextG(S, S1), \+ member(S1, Vizitat), search(S1, [S1 | Vizitat], Path). % 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]). % fiecare afirmatie trebuie să fie adevărată pentru luna sau ziua datei % căutate % A: I don't know when your birthday is s1a(Month) :- dates(Dates), % findall(Date, (member(Date, Dates), % Date = Mth/Day, % Mth = Month), % LDates), % LDates = [_, _ | _]. % sau, mai scurt: findall(_, member(Month/_, Dates), [_, _|_]). % sunt mai multe date în Dates care sunt în luna Month % A: I know Bernard doesn't know, either s1b(Month) :- dates(Dates), forall(member(Month/Day, Dates), % pentru toate datele din Dates care sunt în luna Month, % există mai multe date în Dates care sunt cu aceeasi zi findall(_, member(_/Day, Dates), [_,_|_])). % Bernard: 'I didn't know originally, but now I do.' s2(Day) :- dates(Dates), % este o singură dată în Dates, cu ziua Day, pentru a cărei lună % sunt adevărate afirmatiile s1a si s1b findall(_, (member(Mth/Day, Dates), s1a(Mth), s1b(Mth)), [_]). % Albert: 'Well, now I know, too!' s3(Month) :- dates(Dates), % este o singură dată în Dates, cu luna Month, pentru % a cărei lună sunt adevărate s1a si s1b, si pentru a cărei zi % este adevărat s2 findall(_, (member(Month/Day, Dates), s1a(Month), s1b(Month), s2(Day)), [_]). % solutia este o dată din Dates ale cărei lună / zi respectă afirmatiile % corespunzătoare sol(Date) :- dates(Dates), member(Date, Dates), Date = Mth/Day, s1a(Mth), s1b(Mth), s2(Day), s3(Mth).