For Programmers: Free Programming Magazines  


Home > Archive > Prolog > April 2005 > Problem with this code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with this code
gorans@students.cs.mu.oz.au

2005-04-08, 8:58 am

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

tmp123

2005-04-08, 8:58 am


gorans@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.

gorans@students.cs.mu.oz.au

2005-04-08, 8:58 am

yes,
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.

michael.goodrich@gmail.com

2005-04-13, 8:57 pm


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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com