Code Comments
Programming Forum and web based access to our favorite programming groups.Hi,
I am having a little problem with implementing a feature that is called
from this predicate:
I need to traverse and print the leaves of a binary tree given the
database in the top predicate:
a menu system calls cmd('L', DB, DBnew)
%cmd(+CMD, +DB, -DB)
cmd(0'L, DB, DB) :-
traverse(DB).
%---------
I know that this doesn't work because the menu that calls cmd() will
fail because i do not return the new DB.
How would I modify the above (or the traverse) to enable the cmd() to
evaluate to true?
Traverse is as follows, this works on its own.
traverse(leaf(X)) :-
putl(X), %write to output
nl. %new line
traverse(tree(_, left, right)) :-
traverse(left),
traverse(right).
Thanks in advance
Post Follow-up to this messagegorans@students.cs.mu.oz.au wrote: > Hi, > > ... > > I am having a little problem with implementing a feature that is called > from this predicate: > traverse(tree(_, left, right)) :- > traverse(left), > traverse(right). > > ... Do you mean "Left" instead of "left" and "Right" instead of "right"? Kind regards.
Post Follow-up to this messageyes, the problem wasn't with the traverse predicate. I just quickly copied it so treat it like pseudo code. The problem lies with the relation between cmd(L, DB, DB) and the call to traverse(DB), traverse of DB returns true if the DB is valid.
Post Follow-up to this message
gorans@students.cs.mu.oz.au wrote:
> Hi,
>
> I am having a little problem with implementing a feature that is
called
> from this predicate:
>
> I need to traverse and print the leaves of a binary tree given the
> database in the top predicate:
>
> a menu system calls cmd('L', DB, DBnew)
>
> %cmd(+CMD, +DB, -DB)
>
> cmd(0'L, DB, DB) :-
> traverse(DB).
>
> %---------
> I know that this doesn't work because the menu that calls cmd() will
> fail because i do not return the new DB.
>
> How would I modify the above (or the traverse) to enable the cmd() to
> evaluate to true?
>
>
> Traverse is as follows, this works on its own.
> traverse(leaf(X)) :-
> putl(X), %write to output
> nl. %new line
>
> traverse(tree(_, left, right)) :-
> traverse(left),
> traverse(right).
>
> Thanks in advance
One trick I use which may be applicable is to define a no_op predicate
used with a disjunction:
some_predicate_that you want_to always succeed(X) :-
( some_predicate_which_can_fail(X) ; no_op ).
no_op.
HTH,
-mg
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.