Frisbeetarian
Programmer
Hey all, i am fairly new to Prolog and have had to teach myself the language off the internet as no one on campus had any experience working with Logic Programming.
My question present is a simple one, but it has dumbfounded me for the last 2 hours now. In an excercise, i am asked to find the ancestor of a node in a tree. I've written the code, but i just cant seem to make it give me the right answer.
The Ancestor clause is the one taken from Wikipedia after arduous labor on my Path clause. Ironically, none of the two works . Judging from the Ancestor clause i seem to have the logic pinned down but its syntax or misinformed programming that has held me down. Any help would be tremendously appreciated .
My question present is a simple one, but it has dumbfounded me for the last 2 hours now. In an excercise, i am asked to find the ancestor of a node in a tree. I've written the code, but i just cant seem to make it give me the right answer.
The Ancestor clause is the one taken from Wikipedia after arduous labor on my Path clause. Ironically, none of the two works . Judging from the Ancestor clause i seem to have the logic pinned down but its syntax or misinformed programming that has held me down. Any help would be tremendously appreciated .
Code:
:- op(500, xfx, 'is_parent').
a is_parent b. c is_parent g.
a is_parent c. c is_parent h.
a is_parent d. c is_parent i.
b is_parent e. d is_parent j.
b is_parent f. e is_parent k.
f is_parent l.
f is_parent m.
h is_parent n.
i is_parent o.
i is_parent p.
j is_parent l.
j is_parent r.
j is_parent s.
m is_parent t.
:- op(500, xfx, 'is_ancestor_of').
X is_ancestor_of Y:- ancestor(X,Y).
ancestor(A,B) :- A is_parent X,
ancestor(X, B).
path(K, Node) :- Mother is_parent Node,
path(K, Mother).