0
votes
printtree_3( bt(Label,[],[]) ).
printtree_3( bt(Label,Left,Right) ) :- 
    printtree_3( Left ), 
    printtree_3( Right ), 
    printlabel( Label ).

printlabel( X ) :- write( X ), write( ' ' ).

I am attempting to traverse a tree for the first time in prolog and I thought I would do so by printing a tree with someone else's code to see it work in person. The problem is that trying to run this code I always get "False" by the swipl compiler.

For example.

?- printtree_3(a(b,c,d)).

returns false instead of printing the tree. I think that my predicate call is wrong, but I'm not sure. Again I take no credit for the provided code, I just want to run it so I can experiment. If anyone can explain why it's not working I would be very thankful.

1
Hey guys, I have now tried to call the predicate with printtree_3(bt(a,b,c)). But with no success. I may have missed the point. Can someone just give me what a valid call would look like. - JunDeveloper

1 Answers

1
votes

This fails (returns "false") because the query is false:

You are asking

printtree_3(a(b,c,d))

giving a tree with the functor a

However the code only defines clauses for predicate printtree_3 that accept an argument with functor bt (probably "binary tree" more than "beached thing"):

printtree_3( bt(Label,[],[])      )
printtree_3( bt(Label,Left,Right) )

There is nothing that matches your query. Prolog says "false" (which means there is no evidence that printtree_3(a(b,c,d)) is true)