I have following data:
item(one, 50, 40).
item(two, 80, 70).
item(three, 100, 55).
item(four, 50, 45).
item(five, 50, 40).
item(six, 80, 70).
item(seven, 100, 55).
item(eight, 50, 45).
I use following command to display all values:
findall((A,B,C), item(A,B,C), L).
However, it displays all items. This can be a problem if there are hundred such entries. How can I display a limited number of items, say 3 in above case?
I see there are some posts on this: Prolog: "findall" for limited number of solutions and Finding up to N unique solutions of a goal in Prolog and Prolog: "findall" for limited number of solutions but I am unable to fit those to my problem.
I am trying following code but it is not picking up the length of the list:
head(N):-
findall((A,B,C), item(A,B,C),L),
writeln(L),
writeln("------ 1 ---------"),
length(L, N),
writeln(N),
writeln("------ 2 ---------"),
head2(L, N, 0).
head2(L, N, M):-
M < N,
nth0(M, L, Item),
writeln(Item),
X is M+1,
head2(L, N, X).
Output:
?- head(3).
[ (one,50,40), (two,80,70), (three,100,55), (four,50,45), (five,50,40), (six,80,70), (seven,100,55), (eight,50,45)]
------ 1 ---------
false.