Assignment question about lists using findall and between

findall/3

findall (+Template, :Goal, -Bag)

is part of a family of predicates that is typically used to read data from facts. Each of them have an argument named Goal which is a predicate itself and the facts are read using the Goal predicate. So in your example the Goal would be between(0,Dimension,I) which would mean that there would have to be facts with a functor named between. But between/3 is already a defined predicate with SWI-Prolog, so using between as a functor is not a good choice.

Another bit of advise if you are not sure how a predicate works before using it, take the time to use it by itself to understand how it works so that you are not compounding your problem by introducing more unknowns.

findall examples

value(1).
value(2).
value(3).
value(4).
value(5).
value(6).
value(7).
value(8).

?- findall(Number,value(Number),Numbers).
Numbers = [1, 2, 3, 4, 5, 6, 7, 8].

?- findall(N,(value(N),0 is N mod 2),Evens).
Evens = [2, 4, 6, 8].

The Template tells findall what part of the Goal is needed, and Bag is the name given to collect all of the values returned as Template

So for findall(Number,value(Number),Numbers) the Goal reads the facts value/1 and unifies the number in value/1 with the variable Number in goal. Then since the Template is just Number that is saved, and collected into the variable in Bag which is Numbers.

Some variations that prints the values as they are found

?- findall(N,(value(N),format('N: ~w~n',[N])),Numbers).
N: 1
N: 2
N: 3
N: 4
N: 5
N: 6
N: 7
N: 8
Numbers = [1, 2, 3, 4, 5, 6, 7, 8].

?- findall(N,(value(N),format('N: ~w~n',[N]),0 is N mod 2,format('Even: ~w~n',[N])),Evens).
N: 1
N: 2
Even: 2
N: 3
N: 4
Even: 4
N: 5
N: 6
Even: 6
N: 7
N: 8
Even: 8
Evens = [2, 4, 6, 8].

Also what the other person suggested might be correct, but without seeing the all of the code and knowing the details of the conversation I can’t say if what they said is correct. It is possibly correct, but details are missing.

1 Like