• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
    • RDF quality heuristics
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

4.30 Finding all Solutions to a Goal
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Finding all Solutions to a Goal
          • findall/3
          • findall/4
          • findnsols/4
          • findnsols/5
          • bagof/3
          • setof/3
    • Packages
Availability:built-in
Source[nondet]findnsols(+N, @Template, :Goal, -List)
[nondet]findnsols(+N, @Template, :Goal, -List, ?Tail)
As findall/3 and findall/4, but generates at most N solutions. If N solutions are returned, this predicate succeeds with a choice point if Goal has a choice point. Backtracking returns the next chunk of (at most) N solutions. In addition to passing a plain integer for N, a term of the form count(N) is accepted. Using count(N), the size of the next chunk can be controlled using nb_setarg/3. The non-deterministic behaviour used to implement the chunk option in library(pengines). Based on Ciao, but the Ciao version is deterministic. Portability can be achieved by wrapping the goal in once/1. Below are three examples. The first illustrates standard chunking of answers. The second illustrates that the chunk size can be adjusted dynamically and the last illustrates that no choice point is left if Goal leaves no choice-point after the last solution.
?- findnsols(5, I, between(1, 12, I), L).
L = [1, 2, 3, 4, 5] ;
L = [6, 7, 8, 9, 10] ;
L = [11, 12].

?- State = count(2),
   findnsols(State, I, between(1, 12, I), L),
   nb_setarg(1, State, 5).
State = count(5), L = [1, 2] ;
State = count(5), L = [3, 4, 5, 6, 7] ;
State = count(5), L = [8, 9, 10, 11, 12].

?- findnsols(4, I, between(1, 4, I), L).
L = [1, 2, 3, 4].
ClioPatria (version V3.1.1-40-g9d9e003)