I built the binary tree structure, binarytree(Data,LeftSub,RightSub).
istree(nil).
istree(binarytree(_,L,R)) :- istree(L), istree(R).
However, I want to represent empty tree by uninstantiated variable instead of istree(nil), using built-in predicate var(X).
istree(D) :- var(D).
istree(binarytree(D,_,_)) :- var(D).
istree(binarytree(D,L,R)) :- not(var(D)).
istree(binarytree(D,L,R)) :- not(var(D)), istree(L), istree(R).
I tried but it doesn't seem right.
not(var(D))
tononvar(D)
it seems to be a suitable solution to your needs assuming you do want an uninstantiated variable, on its own, to be considered a valid binary tree (istree(X)
will succeed for any uninstantiated variableX
). – lurker