Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Another little problem

0 views
Skip to first unread message

phant...@hotmail.it

unread,
Feb 26, 2007, 2:18:16 AM2/26/07
to
Hi all, I have another problem :))

I have two variables F and G.
I have a predicate h/3 that perform some operations with F and G and
returns Z.

I want to do a cycle like this:

repeat
h(Z,F,G)
F = G
G = Z
while Z <> empty

I know I have not been very clear, but I don't know how I can explain
this problem. My problem is I don't know how to reuse F and G.

Thanks

Nick Wedd

unread,
Feb 26, 2007, 6:08:30 AM2/26/07
to
In message <1172474296.8...@k78g2000cwa.googlegroups.com>,
phant...@hotmail.it writes

I think I understand what you mean.

This code is untested and may have errors:

hh( InF, InG, InF, InG ) :-
h( empty, InF, InG ).
hh( InF, InG, OutF, OutG ) :-
h( Z, InF, InG ),
hh( InG, Z, OutF, OutG ).

You call hh with the first two arguments instantiated to your initial F
and G. It instantiates its last two arguments to the F and G that
caused Z to become empty - I assume these are the values you are
interested in finding.

Nick

--
Nick Wedd ni...@maproom.co.uk

Thorsten Kiefer

unread,
Feb 26, 2007, 5:45:56 PM2/26/07
to
phant...@hotmail.it wrote:

> repeat
> h(Z,F,G)
> F = G
> G = Z
> while Z <> empty

Hi,
in I think in prolog you have to convert this into recursion.
Which variable holds the result of your computation ? F,G or Z ?
Z makes no sens, as it is [] at the end.
G also makes no sense as it is G := Z at the end.
So I think it is F, right ?
So how about this:

pred(F,F,G) :- h([],F,G).
pred(Fout,F,G) :-
h(Z,F,G),
pred(Fout,G,Z).

Regards
Thorsten

phant...@hotmail.it

unread,
Feb 27, 2007, 2:22:41 AM2/27/07
to
On 26 Feb, 12:08, Nick Wedd <n...@maproom.co.uk> wrote:

> I think I understand what you mean.
>
> This code is untested and may have errors:
>
> hh( InF, InG, InF, InG ) :-
> h( empty, InF, InG ).
> hh( InF, InG, OutF, OutG ) :-
> h( Z, InF, InG ),
> hh( InG, Z, OutF, OutG ).
>
> You call hh with the first two arguments instantiated to your initial F
> and G. It instantiates its last two arguments to the F and G that
> caused Z to become empty - I assume these are the values you are
> interested in finding.
>
> Nick

Thanks Nick, your predicate is great! :)


phant...@hotmail.it

unread,
Feb 27, 2007, 2:24:13 AM2/27/07
to
On 26 Feb, 23:45, Thorsten Kiefer <toki...@usenet.cnntp.org> wrote:

> Hi,
> in I think in prolog you have to convert this into recursion.

I think too, but... i hate the recursion!! :))

> Which variable holds the result of your computation ? F,G or Z ?
> Z makes no sens, as it is [] at the end.
> G also makes no sense as it is G := Z at the end.
> So I think it is F, right ?
> So how about this:
>
> pred(F,F,G) :- h([],F,G).
> pred(Fout,F,G) :-
> h(Z,F,G),
> pred(Fout,G,Z).
>
> Regards
> Thorsten

Thanks Thorsten, i have to try this one.

0 new messages