# Simple Question

**URL:** <https://swi-prolog.discourse.group/t/simple-question/868>\
**Category:** Help!\
**Created:** [June 27, 2019, 11:41am UTC](https://swi-prolog.discourse.group/t/simple-question/868 "2019-06-27T11:41:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![c0rdeiro](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@c0rdeiro](https://swi-prolog.discourse.group/u/c0rdeiro)\
**Post date:** [June 27, 2019, 11:41am UTC](https://swi-prolog.discourse.group/t/simple-question/868/1 "2019-06-27T11:41:31Z")

</div>

I’m using:

SWI-Prolog version online

I want the code to:

So I have predicates of a basketball games in which I save

`(#game,HomeTeam,VisitorTeam,HomePointsOf1stQ,VisitorPointsOf1stQ,...`

so on for the 4 quarters.

I want to see who wins by comparing the sum of each team quarters to get each team final result

But what I’m getting is:

just error

Edit: It’s my first prolog project so I’m a newbie

My code looks like this:

```prolog
statsEquipa:-
    write('What team is it?'), read(Z),
    aggregate_all(count,jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD,VitoriasCasa),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD,VitoriasFora),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD<FA+FB+FC+FD,DerrotasCasa),
    aggregate_all(count,jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD<FA+FB+FC+FD,DerrotasFora),
    CountVitorias is VitoriasCasa+VitoriasFora,
    write(Z), format(": \n"),
    format('Número de Vitórias: '),write(CountVitorias),
    CountDerrotas is DerrotasCasa+DerrotasFora,
    format('\nNúmero de Derrotas: '),write(CountDerrotas).

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 27, 2019, 11:49am UTC](https://swi-prolog.discourse.group/t/simple-question/868/2 "2019-06-27T11:49:27Z")

</div>

Hi welcome.

You say you are using SWI-Prolog on line which I guess is SWISH.

If so can you post the link here and then we can run the code, see the error, etc.

Thanks.

---

<div class="post-metadata">

**Author:** ![c0rdeiro](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@c0rdeiro](https://swi-prolog.discourse.group/u/c0rdeiro)\
**Post date:** [June 27, 2019, 12:00pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/3 "2019-06-27T12:00:18Z")

</div>

I didn’t even get that though ahah thanks

[https://swish.swi-prolog.org/p/Nba.pl](https://swish.swi-prolog.org/p/Nba.pl)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 27, 2019, 12:34pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/4 "2019-06-27T12:34:05Z")

</div>

I copied the source code to my computer, because it is more natural for me to use SWI-Prolog there.

In loading the source into SWI-Prolog using consult/1 I receive the following errors:

```prolog
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:529:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:597:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/9
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:919:
        Clauses of jogo/13 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:528
          Current predicate: jogo/11
          Use :- discontiguous jogo/13. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:920:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:1264:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/10
          Use :- discontiguous jogo/11. to suppress this message

```

The first error

```prolog
Warning: c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:529:
        Clauses of jogo/11 are not together in the source-file
          Earlier definition at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:48
          Current predicate: jogo/13
          Use :- discontiguous jogo/11. to suppress this message

```

Line 48 is the first data line

```prolog
jogo(1,celtics,philadelphia,21,21,26,21,30,24,28,21).

```

and has 11 parameters.

Line 529 is

```prolog
jogo(3,philadelphia,raptors,29,30,36,29,26,20,35,22).

```

and has 11 parameters.

but line 528 is

```prolog
jogo(3,wizards,suns,33,29,22,26,28,24,26,30,40,37).

```

and has 13 parameters.

SWI-Prolog expects all predicates with the same name, e.g. (jogo) and [arity](https://en.wikipedia.org/wiki/Arity) to be listed in order in the code.

The cause of this error message is that the predicate is missing some parameters, or that the predicate has clauses with different arity and the code needs to rearranged to keep the clauses with the same name and arity together.

* * *

If I run the query

```prolog
 ?- statsEquipa.
Qual a equipa?nets.

ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [15] ... + _7042+_7036> ... + _7054+_7048
ERROR: [14] '$bags':findall_loop(jogo(_7100,nets,_7104,_7106,_7108,_7110,_7112,_7114,_7116,_7118,_7120)-1,user:(... > ...),_7088,[]) at c:/program files/swipl/boot/bags.pl:97
ERROR: [13] setup_call_catcher_cleanup('$bags':'$new_findall_bag','$bags':findall_loop(...,...,_7182,[]),_7160,'$bags':'$destroy_findall_bag') at c:/program files/swipl/boot/init.pl:466
ERROR: [9] aggregate:aggregate_all(count,jogo(_7230,nets,_7234,_7236,_7238,_7240,_7242,_7244,_7246,_7248,_7250),user:(... > ...),_7226) at c:/program files/swipl/library/aggregate.pl:258
ERROR: [8] statsEquipa at c:/users/eric/documents/projects/prolog/swi-discourse_001.pl:1287
ERROR: [7] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
^ Exception: (13) setup_call_catcher_cleanup('$bags':'$new_findall_bag', '$bags':findall_loop(jogo(_6104, nets, _6108, _6110, _6112, _6114, _6116, _6118, _6120, _6122, _6124)-1, user:(_6110+_6114+_6118+_6122>_6112+_6116+_6120+_6124), _6198, []), _7340, '$bags':'$destroy_findall_bag') ? creep
^ Call: (15) call('$bags':'$destroy_findall_bag') ?

```

I get that you are extracting the points for each team during each period and adding them for a total for the game, e.g. `CA+CB+CC+CD` but I don’t understand the purpose of `>`, e.g. `CA+CB+CC+CD>FA+FB+FC+FD`.

Please explain.

---

<div class="post-metadata">

**Author:** ![c0rdeiro](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@c0rdeiro](https://swi-prolog.discourse.group/u/c0rdeiro)\
**Post date:** [June 27, 2019, 12:40pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/5 "2019-06-27T12:40:04Z")

</div>

ohh thats a mistake, I wrote over 1000 lines of predicates so I would be afraid that would happen.

So in `CA+CB+CC+CD>FA+FB+FC+FD` I’m trying to compare the final result to see which team has won,  
so CA is HomeTeam points in 1st quarter… and FA is the VisitorTeam points in the 1st quarter and so on, and if the points of CA+CB+CC+CD are greater then (\>) FA+FB+FC+FD then Home Team has won and adds one to ‘VitoriasCasa’ variable

---

<div class="post-metadata">

**Author:** ![c0rdeiro](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@c0rdeiro](https://swi-prolog.discourse.group/u/c0rdeiro)\
**Post date:** [June 27, 2019, 12:44pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/6 "2019-06-27T12:44:55Z")

</div>

> [@EricGT](#):
>
> 28

and the line 528 should be jogo(3,wizards,suns,33,29,22,26,28,24,66,67).

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 27, 2019, 12:48pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/7 "2019-06-27T12:48:42Z")

</div>

Thanks.

Your code was almost correct, mostly just added a few parenthesis in the right place was all.

```prolog
statsEquipa:-
    write('Qual a equipa?'), read(Z),
    aggregate_all(count, (jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) > (FA+FB+FC+FD)),VitoriasCasa), % Home victories
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) > (FA+FB+FC+FD)),VitoriasFora), % Away victories
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) < (FA+FB+FC+FD)),DerrotasCasa), % Home defeats
    aggregate_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) < (FA+FB+FC+FD)),DerrotasFora), % Away defeats
    CountVitorias is VitoriasCasa+VitoriasFora, % Victories
    CountDerrotas is DerrotasCasa+DerrotasFora, % Defeats
    format('~w: ~nNúmero de Vitórias: ~w~nNúmero de Derrotas: ~w~n',[Z,CountVitorias,CountDerrotas]).

```

[aggregrate\_all/3](https://www.swi-prolog.org/pldoc/man?predicate=aggregate_all/3) takes three arguements, but

```prolog
jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),CA+CB+CC+CD>FA+FB+FC+FD

```

is seen as more than one. It just needed some parenthesis to make it one argument.  
Also added parenthesis around `CA+CB+CC+CD` so the compares worked correctly.

Lastly just used format correctly. I too had a lot of problems understanding format at first, but now it comes easy. 😃

```prolog
?- statsEquipa.
Qual a equipa?nets.
nets: 
NÃºmero de VitÃ³rias: 45
NÃºmero de Derrotas: 38
true.

```

Also I don’t know Spanish so hopefully the words are correct.

---

<div class="post-metadata">

**Author:** ![c0rdeiro](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@c0rdeiro](https://swi-prolog.discourse.group/u/c0rdeiro)\
**Post date:** [June 27, 2019, 12:55pm UTC](https://swi-prolog.discourse.group/t/simple-question/868/8 "2019-06-27T12:55:12Z")

</div>

> [@EricGT](#):
>
> statsEquipa:- write(‘Qual a equipa?’), read(Z), aggregate\_all(count, (jogo(X,Z,Y,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) \> (FA+FB+FC+FD)),VitoriasCasa), Home victories aggregate\_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) \> (FA+FB+FC+FD)),VitoriasFora), Away victories aggregate\_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) \< (FA+FB+FC+FD)),DerrotasCasa), Home defeats aggregate\_all(count, (jogo(X,Y,Z,CA,FA,CB,FB,CC,FC,CD,FD),(CA+CB+CC+CD) \< (FA+FB+FC+FD)),DerrotasFora), Away defeats CountVitorias is VitoriasCasa+VitoriasFora, Victories CountDerrotas is DerrotasCasa+DerrotasFora, Defeats format(‘~w: ~nNúmero de Vitórias: ~w~nNúmero de Derrotas: ~w~n’,[Z,CountVitorias,CountDerrotas]).

omg thank you so much!  
It’s actually portuguese but yeah 😛
