JESS: [EXTERNAL] nested foreach to return from the inner loop

"Przemyslaw Woznowski" <[email protected]> Thu, 23 May 2013 17:43:26 +0100
Newsgroups gmane.comp.java.jess
Message-ID <CA+MrVi_DwpyNJzNzxTdJZtJ6dhTCYfwDR2UGZtpOSc_g=XYsaA@mail.gmail.com>
--001a11c36bdca7748904dd656166
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
 charset=iso-8859-1

Hi Jess users,

I have the following function:

(deffunction similarity-score (?base ?other)
    (bind ?score 0)
    (bind ?totalDistance 0)
    (bind ?counter 0)
    (foreach ?x ?base
        (bind ?counter (+ ?counter 1))
        (printout t "counter is:" ?counter)
        (bind ?partialDistance 0)
        (printout t "partialDist is:" ?partialDistance)
        (foreach ?y ?other
            (bind ?partialDistance (+ ?partialDistance 1))
            (printout t "foreach partialDist is:" ?partialDistance)
            (if (eq ?x ?y) then
                (bind ?score (+ ?score 1))
                (printout t "score is:" ?score)
                (return)
            )
        )
    (bind ?totalDistance (/ ?counter ?partialDistance))
    )
    (return (- (/ ?score (length$ ?base)) (* (- 1 (/ (length$ ?base)
?totalDistance)) 0.1) ))
)

As you can see, the above function has a nested foreach loop, which upon
finding equal values in both list should skip to the next iteration of the
outer loop. In java, instead of (return) one would normally put a break
statement. As far as the documentation of the (foreach) construct reads, it
says: "The return function can be used to break the iteration". However,
what I am finding is that the (return) function terminates the outer loop
too - unless I seriously messed up the code - but this is the result of my
observation of the (printout t) function. Moreover, when I remove the
(return) function, the function iterates over the outer and inner loops
just fine.

Any advice on how can I break the inner's loop iteration? I know I can use
a variable to flag that the match has been found and add it to the if
statement, but I am hoping that there is an equivalent of break in Jess.


Cheers,
Pete

--001a11c36bdca7748904dd656166
Content-Type: text/html;
 charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><div>Hi Jess users,<br><br></div>I have the=
 following function:<br><br>(deffunction similarity-score (?base ?other)<br=
>=A0=A0=A0 (bind ?score 0)<br>=A0=A0=A0 (bind ?totalDistance 0)<br>=A0=A0=
=A0 (bind ?counter 0)<br>
=A0=A0=A0 (foreach ?x ?base<br>=A0=A0=A0 =A0=A0=A0 (bind ?counter (+ ?count=
er 1))<br>=A0=A0=A0 =A0=A0=A0 (printout t &quot;counter is:&quot; ?counter)=
<br>=A0=A0=A0 =A0=A0=A0 (bind ?partialDistance 0)<br>=A0=A0=A0 =A0=A0=A0 (p=
rintout t &quot;partialDist is:&quot; ?partialDistance)<br>
=A0=A0=A0 =A0=A0=A0 (foreach ?y ?other<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 (bi=
nd ?partialDistance (+ ?partialDistance 1))<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=
=A0 (printout t &quot;foreach partialDist is:&quot; ?partialDistance)<br>=
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 (if (eq ?x ?y) then <br>=A0=A0=A0 =A0=A0=A0 =
=A0=A0=A0 =A0=A0=A0 (bind ?score (+ ?score 1))<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 (printout t &quot;score is:&quot; ?=
score)<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 (return)<br>=A0=A0=A0 =A0=
=A0=A0 =A0=A0=A0 )<br>=A0=A0=A0 =A0=A0=A0 )<br>=A0=A0=A0 (bind ?totalDistan=
ce (/ ?counter ?partialDistance))<br>=A0=A0=A0 )<br>=A0=A0=A0 (return (- (/=
 ?score (length$ ?base)) (* (- 1 (/ (length$ ?base) ?totalDistance)) 0.1) )=
)<br>
)<br><br></div>As you can see, the above function has a nested foreach loop=
, which upon finding equal values in both list should skip to the next iter=
ation of the outer loop. In java, instead of (return) one would normally pu=
t a break statement. As far as the documentation of the (foreach) construct=
 reads, it says: &quot;The return function can be used to break the iterati=
on&quot;. However, what I am finding is that the (return) function terminat=
es the outer loop too - unless I seriously messed up the code - but this is=
 the result of my observation of the (printout t) function. Moreover, when =
I remove the (return) function, the function iterates over the outer and in=
ner loops just fine.<br>
<br></div>Any advice on how can I break the inner&#39;s loop iteration? I k=
now I can use a variable to flag that the match has been found and add it t=
o the if statement, but I am hoping that there is an equivalent of break in=
 Jess.<br>
<br><br clear=3D"all"></div>Cheers,<br>Pete<br></div>

--001a11c36bdca7748904dd656166--