For in range

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

So I have a league classification of NBA which is divided by conference east and west and I want to grab the first 8 of each one, I have no idea how to do a for in range what is the best way?

You need to give an example of the expected response.

@EricGT

So the classification is

team(east,bucks).
team(east,raptors).
team(east,philadelphia).
team(east,celtics).
team(east,pacers).
team(east,nets).
team(east,magic).
team(east,pistons).
team(east,hornets).
team(east,heat).
team(east,wizards).
team(east,hawks).
team(east,bulls).
team(east,cavaliers).
team(east,knicks).

team(west,warriors).
team(west,nuggets).
team(west,blazers).
team(west,rockets).
team(west,utah).
team(west,thunder).
team(west,spurs).
team(west,clippers).
team(west,sacramento).
team(west,lakers).
team(west,timberwolves).
team(west,grizzlies).
team(west,pelicans).
team(west,mavericks).
team(west,suns).

and I want a procedure to return the first 8 of each one so it would be:

team(east,bucks).
team(east,raptors).
team(east,philadelphia).
team(east,celtics).
team(east,pacers).
team(east,nets).
team(east,magic).
team(east,pistons).

team(west,warriors).
team(west,nuggets).
team(west,blazers).
team(west,rockets).
team(west,utah).
team(west,thunder).
team(west,spurs).
team(west,clippers).

Edit: Idk why but I am having a hard time using the spoiler thing of prolog here
Edit2: also, this is my last question, then my project is done :slight_smile:

first_8(Conference,Teams) :-
    bagof(Team,team(Conference,Team),All_teams),
    take(8,All_teams,Teams).

take(N, _, Xs) :- N =< 0, !, N =:= 0, Xs = [].
take(_, [], []).
take(N, [X|Xs], [X|Ys]) :- M is N-1, take(M, Xs, Ys).

take/3

?- first_8(east,Teams).
Teams = [bucks, raptors, philadelphia, celtics, pacers, nets, magic, pistons].

?- first_8(west,Teams).
Teams = [warriors, nuggets, blazers, rockets, utah, thunder, spurs, clippers].
1 Like

@EricGT thank you so much for all the support, I’ll mention you in the paper :smiley:

Wasn’t expecting that. I was just helping you because that gives Jan more time to help others like me. If you want to thank someone in the paper then thank Jan for his work with SWI-Prolog.

1 Like

Taking Jan’s advice; never used these before.

first_8_2(Conference,Teams) :-
    aggregate_all(bag(Team),(limit(8,team(Conference,Team))),Teams).
?- first_8_2(east,Teams).
Teams = [bucks, raptors, philadelphia, celtics, pacers, nets, magic, pistons].
2 Likes