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
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
> 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
> 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! :)
> 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.