# Re: [eclipse-clp-users] Loop Interaction

From: Joachim Schimpf <joachim.schimpf_at_...44...>
Date: Thu, 22 Apr 2010 13:50:35 +1000
```Stefan Friese wrote:
> Hello,
>
> it looks like you are asking for a nested loop, maybe something like
> this (i did not test these lines):
>
> %Xs = [(X1,Y1),(X2,Y2),(X3,Y3)]
> %NFs = [NFAB, NFAC, NFBA, NFBC, NFCA, NFCB]
>        (foreach(Pair1,Xs),
>         param(Xs),
>         param(NFs)
>        do
>           (foreach(Pair2,Xs),
>            foreach(NF,NFs)
>           do
>             (Pair1 \== Pair2 ->
>               outside_polygon1(Pair1,Pair2,NF)
>             ;
>               true
>             )
>           )
>        )

Close, but you are not collecting the NFs correctly.  You would have to
use one of the following two patterns from the reference manual page
for do/2 (http://eclipse-clp.org/doc/bips/kernel/control/do-2.html):

% Find all pairs of list elements and collect them in a result list Zs:
pairs(Xs, Ys, Zs) :-
(
foreach(X,Xs),
fromto(Zs, Zs4, Zs1, []),
param(Ys)
do
(
foreach(Y,Ys),
fromto(Zs4, Zs3, Zs2, Zs1),
param(X)
do
Zs3 = [X-Y|Zs2]
)
).

% or, equivalent
pairs(Xs, Ys, Zs) :-
(
foreach(X, Xs) * foreach(Y, Ys),
foreach(Z, Zs)
do
Z = X-Y
).

Using the second, pattern, Igor's code would become:

Igor Kondrasovas wrote:
>
> solve([(X1,Y1),(X2,Y2),(X3,Y3)], [NFAB, NFAC, NFBA, NFBC, NFCA, NFCB]):-
>        [X1,X2,Y1,Y2,X3,Y3] :: -10.. 10,
>        outside_polygon1((X1,Y1), (X2,Y2), NFAB),
>        outside_polygon1((X1,Y1), (X3,Y3), NFAC),
>        outside_polygon1((X2,Y2), (X1,Y1), NFBA),
>        outside_polygon1((X2,Y2), (X3,Y3), NFBC),
>        outside_polygon1((X3,Y3), (X1,Y1), NFCA),
>        outside_polygon1((X3,Y3), (X2,Y2), NFCB),
>        search([X1,X2,Y1,Y2,X3,Y3], 0, first_fail, indomain_split, complete, []).

solve(Points, NFs) :-
( foreach((X,Y),Points), fromto(XYs,[X,Y|XYs1],XYs1,[]) do true ),
XYs :: -10..10,
(
foreach(Point1,Points) * foreach(Point2,Points),
fromto(NFs,NFs1,NFs2,[])
do
( Point1 \== Point2 ->
NFs1 = [NF|NFs2],
outside_polygon1(Point1, Point2, NF)
;
NFs1 = NFs2
)
),
search(XYs, 0, first_fail, indomain_split, complete, []).

-- Joachim
```
Received on Thu Apr 22 2010 - 03:48:56 CEST

This archive was generated by hypermail 2.3.0 : Tue May 14 2019 - 03:13:24 CEST