# Some newbie questions (e.g. how to get at most N solutions, what is template? what is @, :, +, - in arguments)

**URL:** <https://swi-prolog.discourse.group/t/some-newbie-questions-e-g-how-to-get-at-most-n-solutions-what-is-template-what-is-in-arguments/4637>\
**Category:** Help!\
**Created:** [November 5, 2021, 9:04am UTC](https://swi-prolog.discourse.group/t/some-newbie-questions-e-g-how-to-get-at-most-n-solutions-what-is-template-what-is-in-arguments/4637 "2021-11-05T09:04:43Z")\
**Posts on this page:** 1\
**Showing post:** 9

<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:** [November 5, 2021, 10:40am UTC](https://swi-prolog.discourse.group/t/some-newbie-questions-e-g-how-to-get-at-most-n-solutions-what-is-template-what-is-in-arguments/4637/9 "2021-11-05T10:40:37Z")

</div>

> [@chansey97](#):
>
> I am looking for the equivalent in [swi-prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual).

Perhaps this

```prolog
?- bagof(item(X,Y),test(X,Y),Items).
Items = [item(1, 1), item(1, 2), item(2, 1), item(2, 2)].

```

or this

```prolog
?- bagof(item(Y),test(1,Y),Items).
Items = [item(1), item(2)].

```

* * *

**EDIT**

An [answer](https://stackoverflow.com/a/20866206/1243762) by [false](https://stackoverflow.com/users/772868/false) from StackOverflow seems to be what you seek.

```prolog
findfirstn(N, Template, Goal_0, Instances) :-
   findall(Template, call_firstn(Goal_0, N), Instances).

call_firstn(Goal_0, N) :-
   N + N mod 1 >= 0, % ensures that N >=0 and N is an integer
   call_nth(Goal_0, Nth),
   ( Nth == N -> ! ; true ).

```

Complete example

```prolog
:- module(examples,
    [
        findfirstn/4
    ]).

test(1,a).
test(2,b).
test(3,c).
test(4,d).

findfirstn(N, Template, Goal_0, Instances) :-
    findall(Template, call_firstn(Goal_0, N), Instances).

 call_firstn(Goal_0, N) :-
    N + N mod 1 >= 0, % ensures that N >=0 and N is an integer
    call_nth(Goal_0, Nth),
    ( Nth == N -> ! ; true ).

```

Example run

```prolog
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.0)

?- working_directory(_,'C:/Users/Groot').
true.

?- [examples].
true.

?- findfirstn(2,item(X,Y),test(X,Y),Items).
Items = [item(1, a), item(2, b)].

?- findfirstn(3,item(X,Y),test(X,Y),Items).
Items = [item(1, a), item(2, b), item(3, c)].

?- findfirstn(1,item(X,Y),test(X,Y),Items).
Items = [item(1, a)].

?- findfirstn(5,item(X,Y),test(X,Y),Items).
Items = [item(1, a), item(2, b), item(3, c), item(4, d)].

```

* * *

**EDIT**

Knew this predicate was somewhere just could not remember the name because I never use it, limit/2. ([source](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/solution_sequences.pl?show=src#limit/2))

```prolog
?- bagof(test(X,Y),limit(2,examples:test(X,Y)),Items).
Items = [test(1, a), test(2, b)].

?- bagof(test(X,Y),limit(10,examples:test(X,Y)),Items).
Items = [test(1, a), test(2, b), test(3, c), test(4, d)].

```

---

_[View the full topic](https://swi-prolog.discourse.group/t/some-newbie-questions-e-g-how-to-get-at-most-n-solutions-what-is-template-what-is-in-arguments/4637)._
