prolog - Undefined procedure when procedure is defined -
i trying define simple binary search tree. stored in lists so: [key, left tree, right tree]. believe have done when try use bstadd on existing tree following error.
?- bstadd(19,[],t1), bstadd(9, t1, t2). error: bstadd/3: undefined procedure: right/3 exception: (8) right(9, [[], []], _g3233) ?
i have defined right 3 arugments on line 8. follows code:
% bstadd(key, tree, newtree) % add element key tree tree , return % new tree newtree. element in left subtree l must less key , % elements in right subtree r must greater key. means duplicates % not allowed in binary search tree. don’t put print statements in % predicate. right(key, [treekey|treetail], [treekey|newtree]) :- grabtail(key, treetail, newtree]). grabtail(key, [treekey|_], [treekey|newtree]) :- bstadd(key, treekey, newtree). bstadd(key, [], [key,[],[]]). bstadd(key, [treekey|treetail], [treekey|newtree]) :- key > treekey, grabtail(key, treetail, newtree). bstadd(key, [treekey|treetail], [treekey|newtree]) :- key < treekey, right(key, treetail, newtree). % inorder(tree) % given binary search tree tree perform inorder traversal of % tree printing (use print(x) ) value of each vertex inorder. inorder([treehead|treetail]) :- inright(treetail), print(treehead), intail(treetail). inright([_|treetail]) :- intail(treetail). intail([treehead|_]) :- inorder(treehead).
any , insight appreciated.
always systems says while loading file. sure there kind of syntax error. in case, right/3
contains syntax error @ end. there unmatched closing ]
.
right(key, [treekey|treetail], [treekey|newtree]) :- grabtail(key, treetail, newtree]). ^^^
Comments
Post a Comment