(
for(I,1,N),
param(I,Number ,Space)
do
(
for(J,1,M),
param(Number ,Space)
do
Number #= Space[I,J]
),
),
But it doesn't work (problem with ","). But I'm sure it's possible
more simply with a special function (I tried multifor but it doesn't
seems to do what I want)
Does someone know how ? or see what's the problem in my code ?
thk you for you help.
In SWI-Prolog, there's no for structure but foreach/2 predicate that
finds a goal for every elements of a source. And you can write your
own for/3 predicate, by defining it semantics. However before it you
ought to know some build-in predicates and mechanism of Prolog,
including repeat/0, cut and backtracking mechanisms, and et al.
rect(N,M) :-
NM is N*M,
dim(Space, [N,M]),
Space:: 1..NM,
Number is 3,
( for(I,1,N), param(I,Number ,Space) do
( for(J,1,M), param(Number ,Space) do
SquareSpace is Space[I,J],
Number #= SquareSpace
),
),
...
How can I do that without using this method (who doesn't work
because : syntax error : unexpected closing bracket) ?
> Number #= SquareSpace
> ),
This "," should be removed.
Best regards
--
[ Wit Jakuczun http://www.linkedin.com/in/jakuczunwit ]
I'm sorry for that.
Commas in Prolog are separators, not terminators.
So if nothing follows, don't put a comma.
> ),
> ...
>
> How can I do that without using this method (who doesn't work
> because : syntax error : unexpected closing bracket) ?
You are probably using ECLiPSe, which means you can find the
loop documentation with many examples at
http://eclipse-clp.org/doc/bips/kernel/control/do-2.html
and examples of small constraint programs at
http://eclipse-clp.org/examples
many of which containing similar patterns.
The param() construct is needed when you want to access a variable
from outside the loop inside the loop body. You have done that
correctly with Number and Space, but you need to pass M through
the outer loop boundary, and I through the inner loop boundary:
( for(I,1,N), param(M,Number,Space) do
( for(J,1,M), param(I,Number,Space) do
Number #= Space[I,J]
)
)
ECLiPSe also provides shortcuts for such nested loop patterns:
( multifor([I,J],[1,1],[N,M]), param(Number,Space) do
Number #= Space[I,J]
)
or more general
( for(I,1,N)*for(J,1,M), param(Number,Space) do
Number #= Space[I,J]
)
-- Joachim