For Programmers: Free Programming Magazines  


Home > Archive > Prolog > February 2007 > Another little problem









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 Another little problem
phantastic@hotmail.it

2007-02-26, 4:11 am

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

2007-02-26, 8:06 am

In message <1172474296.866580.192090@k78g2000cwa.googlegroups.com>,
phantastic@hotmail.it writes
>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.


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 nick@maproom.co.uk
Thorsten Kiefer

2007-02-26, 7:07 pm

phantastic@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

phantastic@hotmail.it

2007-02-27, 4:15 am

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


phantastic@hotmail.it

2007-02-27, 4:15 am

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.

Sponsored Links







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

Copyright 2008 codecomments.com