Saturday, April 17, 2010

Prolog

http://ubuntuforums.org/showthread.php?t=159367

$ apt-cache search prolog

http://www.cs.bris.ac.uk/Teaching/Resources/COMS30106/labs/tracer.html

----- start my-prog.pl -----
link(a,b).
link(c,d).
link(b,c).
path(X,Z) :- link(X,Z).
path(X,Z) :- link(Y,Z), path(X,Y).
----- end my-prog.pl -----

? trace.
? [my-prog].
? path(a,d).
true
? path(a,e).
false

http://www.cse.ucsc.edu/classes/cmps112/Spring03/languages/prolog/PrologIntro.pdf

---------------------------
append([],List,List).
append([H|Tail],X,[H|NewTail]) :- append(Tail,X,NewTail).
---------------------------
?- trace. append([a,b,c],[d,e],X).

Unknown message: query(yes)
Call: (7) append([a, b, c], [d, e], _G329) ? creep
Call: (8) append([b, c], [d, e], _G410) ? creep
Call: (9) append([c], [d, e], _G413) ? creep
Call: (10) append([], [d, e], _G416) ? creep
Exit: (10) append([], [d, e], [d, e]) ? creep
Exit: (9) append([c], [d, e], [c, d, e]) ? creep
Exit: (8) append([b, c], [d, e], [b, c, d, e]) ? creep
Exit: (7) append([a, b, c], [d, e], [a, b, c, d, e]) ? creep
X = [a, b, c, d, e].

1 Call append([a,b,c],[d,e],Result). %% Initial call
%% Result = [a|NewTail]
2 Call append([b,c],[d,e],NewTail). %% First recursive call
%% NewTail = [b|NewTail1]
3 Call append([c],[d,e],NewTail1). %% Second recursive call
%% NewTail1 = [c|NewTail2]
4 Call append([],[d,e],NewTail2). %% Third recursive call
5 exit NewTail2 = [d,e] %% Unifying with base case
%% of append.
6 exit NewTail1 = [c,d,e] %% Back substitution of
%% NewTail2
7 exit NewTail = [b,c,d,e] %% Back substitution of
%% NewTail1
8 exit Result = [a,b,c,d,e] %% Back substitution of
%% NewTail

Programs
http://www.anselm.edu/homepage/mmalita/culpro/index.html

Palindrome
palindrome(L):- reverse(L,L).

my_reverse([],[]).
my_reverse([H|T],R):- my_reverse(T,T1),append(T1,[H],R).
?- palindrome([a,b,b,a]).
true

No comments: