Pass condition

I’m using:

SWI-Prolog version online https://swish.swi-prolog.org/p/NBA.pl

I want the code to:

So I am doing a foreach and then I got an if inside it, so when my condition is true it will print something, but when its false it stops iterating the foreach

How do I do a pass condition whenever it returns false so I can move on iterating the rest

My code looks like this:

jogosPerdidos(Equipa):-
    format('Perdeu com:\n'),
    format('Fora:'),
    foreach(jogo(_,X,Equipa,CA,FA,CB,FB,CC,FC,CD,FD),
            ((CA+CB+CC+CD)<(FA+FB+FC+FD) -> (format('\n'),write(X)))),
    format('\nEm casa: '),
    foreach(jogo(_,Equipa,X,CA,FA,CB,FB,CC,FC,CD,FD),
            ((CA+CB+CC+CD) < (FA+FB+FC+FD) -> (format('\n'),write(X)))).
jogosPerdidos(Equipa) :-
    format('Perdeu com:\n'),
    format('Fora:'),
    foreach(jogo(_,X,Equipa,CA,FA,CB,FB,CC,FC,CD,FD),
                (
                    (CA+CB+CC+CD)<(FA+FB+FC+FD)
                ->
                    (format('\n'),write(X))
                ;
                    true
                )
            ),
    format('\nEm casa: '),
    foreach(jogo(_,Equipa,X,CA,FA,CB,FB,CC,FC,CD,FD),
                (
                    (CA+CB+CC+CD) < (FA+FB+FC+FD)
                ->
                    (format('\n'),write(X))
                ;
                    true
                )
            ).

Basically this adds an else case that is always true.

1 Like

You want the forall/2 predicate, not the foreach/2 predicate. With forall/2, you simple write e.g.

forall(
    (   jogo(_,X,Equipa,CA,FA,CB,FB,CC,FC,CD,FD),
        CA+CB+CC+CD < FA+FB+FC+FD
    ),
    (format('\n'),write(X))
), ...
2 Likes