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

how to do" for in for" in prolog ?

3 views
Skip to first unread message
Message has been deleted

Bob

unread,
Nov 21, 2009, 4:08:11 PM11/21/09
to
Hy!
I want to do something like this in prolog :

(
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.

YauHsienHuang

unread,
Nov 21, 2009, 11:48:02 PM11/21/09
to
The for loop is not a general language feature. The fact that in
languages like C or Java there is a for structure may causes one's
concept that any language must have a for structure while if-else
statement and assignment. But it's not true for all languages. If
languages are divided into three parts, imperative languages (C or
Java) occupies only one part. You cannot force Prolog to have the for
structure. Iteration is embedded in the evaluation of Prolog
toplevel, so it need not have a for statement.

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.

Bob

unread,
Nov 22, 2009, 8:13:35 AM11/22/09
to
Ok, but if I want all my squares having a value of 3, for example :

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) ?

Wit Jakuczun

unread,
Nov 22, 2009, 8:53:42 AM11/22/09
to
On 22 Lis, 14:13, Bob <beber...@gmail.com> wrote:


>                 Number #= SquareSpace
>             ),
This "," should be removed.

Best regards
--
[ Wit Jakuczun http://www.linkedin.com/in/jakuczunwit ]

YauHsienHuang

unread,
Nov 22, 2009, 11:12:19 AM11/22/09
to
Ah, a specific question got a general answer. It's my fault.

I'm sorry for that.

Joachim Schimpf

unread,
Nov 22, 2009, 7:01:28 PM11/22/09
to
Bob wrote:
> Ok, but if I want all my squares having a value of 3, for example :
>
> 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
> ),

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

0 new messages