11 maio 2017

Listing items when these items are also lists

A way to list items which also are lists is by "nth" internal action asking for each register. Having the register another "nth" call may get the fields of the register like this:

products([

["Banana",1], 
["Apple",2], 
["Pinapple",2.5]
]).
!start.

+!start : products(List) <-
.length(List,LLenght);
-+listSize(0);
while(listSize(Sz) & Sz < LLenght)
{        
.random(Y);
.nth(Sz,List,Item);
.nth(0,Item,Name);
.nth(1,Item,Price);
.print("Product(",Sz,"): ",Name," $",Price);
-+listSize(Sz+1);
    }.

Another way is using "member" internal action which returns with each item of a list (which can be seen as a register if it is a two dimensions array):

products([
["Banana",1], 
["Apple",2], 
["Pinapple",2.5]
]).
!start.

+!start : products(List) <-
for (.member(Item,List)){
.random(Y);
.nth(0,Item,Name);
.nth(1,Item,Price);
.print("Product: ",Name," $",Price);
}.