Commit 18d5853c authored by Katja Bercic's avatar Katja Bercic

Upload New File

parent fd19a5e6
% HW3 Uninformed Search: Definitions and Hints
% First, we define trees as in the exercise sheet:
subtrees([]).
subtrees([(Cost,T)|Rest]) :- number(Cost),istree(T), subtrees(Rest).
istree(tree(Value,Children)) :- string(Value),subtrees(Children).
% We'll add a couple of test trees:
testTree1(tree("A",[])).
testTree2(tree("A",[(2,tree("B",[(5,tree("C",[]))])),(3,tree("B",[(10,tree("D",[]))]))])).
testTree3(tree("A",[(2,tree("B",[(5,tree("C",[])),(60,tree("D",[])),(42,tree("H",[]))])),(3,tree("C",[(10,tree("E",[(60,tree("J",[(60,tree("D",[(60,tree("I",[]))]))]))]))]))])).
testTree4(tree("A",[(2,tree("B",[(5,tree("C",[]))])),(3,tree("C",[(10,tree("D",[]))]))])).
treeZ(tree("Z",[])).
treeY(tree("Y",[])).
treeX(tree("X",[])).
treeW(tree("W",[])).
treeV(tree("V",[])).
treeU(tree("U",[])).
treeT(tree("T",[])).
treeS(tree("S",[])).
treeR(tree("R",[(5,TreeZ)])) :- treeZ(TreeZ).
treeQ(tree("Q",[])).
treeP(tree("P",[])).
treeO(tree("O",[])).
treeN(tree("N",[])).
treeM(tree("M",[(21,TreeY)])) :- treeY(TreeY).
treeL(tree("L",[(30,TreeX)])) :- treeX(TreeX).
treeK(tree("K",[(28,TreeW)])) :- treeW(TreeW).
treeJ(tree("J",[(14,TreeV)])) :- treeV(TreeV).
treeI(tree("I",[(8,TreeT),(7,TreeU)])) :- treeT(TreeT), treeU(TreeU).
treeH(tree("H",[(6,TreeS)])) :- treeS(TreeS).
treeG(tree("G",[(14,TreeQ),(3,TreeR)])) :- treeQ(TreeQ), treeR(TreeR).
treeF(tree("F",[(18,TreeP)])) :- treeP(TreeP).
treeE(tree("E",[(3,TreeN),(12,TreeO)])) :- treeN(TreeN), treeO(TreeO).
treeD(tree("D",[(3,TreeK),(17,TreeL),(20,TreeM)])) :- treeK(TreeK),treeL(TreeL),treeM(TreeM).
treeC(tree("C",[(14,TreeH),(8,TreeI),(11,TreeJ)])) :- treeH(TreeH),treeI(TreeI),treeJ(TreeJ).
treeB(tree("B",[(3,TreeE),(1,TreeF),(2,TreeG)])) :- treeE(TreeE),treeF(TreeF),treeG(TreeG).
treeA(tree("A",[(5,TreeB),(6,TreeC),(4,TreeD)])) :- treeB(TreeB),treeC(TreeC),treeD(TreeD).
% Check that they work
% ?- testTree1(A), istree(A), testTree2(B), istree(B), testTree3(C), istree(C), treeA(NA), istree(NA).
% - - - - - - - - - - - - - - - - - - - - - - - -
% The following might be useful
% - - - - - - - - - - - - - - - - - - - - - - - -
% Unifying terms
treeRoot(Tree) :-
Tree = tree(Root,_), % unify Term1 with Term2
write(Root).
% ?- testTree1(T), treeRoot(T).
% String concatenation
% ?- string_concat("a","b",S).
% Semicolon: an "or" in Prolog
tomJerry(X) :- X = "Tom"; X = "Jerry".
% ?- findall(X,tomJerry(X),Y).
% Appending
% ?- append(["a","b"],["c"],L).
% Sorting
% ?- sort([3,5,2,6,4,1],L)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment