Using foreach/2

Boris Vassilev <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CAFw8osL2H6v0COjnzOO1sA8XR6Ry8ovusqdKh=8JOJB00RHzUA@mail.gmail.com>
Hello,

I am having trouble understanding the implications of using `foreach/2`
from `library(aggregate)`.

Given a "state", represented as a term of arbitrary length, I want to find
a new state, a term with the same functor, based on some rules. The rules
themselves use (arbitrary) elements from the current state.

While I don't know the arity of my state functor, it will not change during
a program run.

The reason why I chose to use a term for representing the state is that I
don't want to worry about efficiently finding the arguments that a rule
might need: I use `arg/3` instead (I am under the impression that it has
efficient and constant time access to term arguments, when its first
argument is ground).

Now the question: if I would define `update_state/2` like this:

    update_state(Current_state, Next_state) :-
        functor(Current_state, state, Arity),
        functor(Next_state, state, Arity),
        foreach(
            (   between(1, Arity, N),
                next_arg_state(Current_state, N, A) % defined elsewhere....
            ),
            arg(N, Next_state, A)
        ).

Would that be a silly thing to do? How should I do this instead?
(I know it does the job, but I admit that I don't fully understand the
implications of `foreach/2` repeatedly copying its second argument `Goal`.
The obvious other method is to use `=..` and `maplist`.)

Thank you for the help,
Boris
-------------- next part --------------
HTML attachment scrubbed and removed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.