I have been stuck on this problem for a couple of days. So given a balanced tree of the form t(Tree, Value, Tree), I want to find the depth of the left tree, which I did with the following code:
depthLeft(nil, 1).
depthLeft(t(Tree1, _, _), N) :-
depthLeft(Tree1, N1),
N is N1 + 1.
So the code works but I'm very confused at the counter. Firstly why can't you put the counter above the recursive call? Secondly why can't you argue that N1 is N - 1 instead like here to return the nth element of a list:
element_at(X,[X|_],1).
element_at(X,[_|L],K) :- K > 1, K1 is K - 1, element_at(X,L,K1).
I fail to see why the counters have a different logic here. If anyone could point me in the direction it would be greatly appreciated.