Loogilise programmeerimise meetod
Kontrolltöö
(Lahendite leidmine)
Kirjeldage Prologi tööd kõigi lahendite leidmisel.
p([],_Ys).
p([X|Xs],[X|Ys]):-p(Xs,Ys).
?-p(Xs,[a,b]).
(Aritmeetika)
Kirjutage programm, mis leiab esimese n arvu ruutude summa.
?-sum(5,55).
(Keerdülesanne)
Leidke Prologi abil 3*3 ruut, mille igas lahtris on erinev arv 1,2,...,9 ning mille kõigi
ridade, veergude ja diagonaalide summa on sama.
?-magic(A1,A2,A3,B1,B2,B3,C1,C2,C3).
(Listid)
Kirjutage programm, mis kustutab listist negatiivsed arvud.
?-delneg([1,-2,-4,3],[1,3]).
Lahendused
%1. Lahendite leidmine
?- trace, p(Xs,[a,b]).
Call: (6) p(_G336, [a, b]) ? creep
Exit: (6) p([], [a, b]) ? creep
Xs = [] ;
Redo: (6) p(_G336, [a, b]) ? creep
Call: (7) p(_G390, [b]) ? creep
Exit: (7) p([], [b]) ? creep
Exit: (6) p([a], [a, b]) ? creep
Xs = [a] ;
Redo: (7) p(_G390, [b]) ? creep
Call: (8) p(_G393, []) ? creep
Exit: (8) p([], []) ? creep
Exit: (7) p([b], [b]) ? creep
Exit: (6) p([a, b], [a, b]) ? creep
Xs = [a, b] ;