Adding items to a list in Prolog

I want to extract items from a list and add them to another new list. How can i do this -

L=[['abc',18],['bcd',19],['def',20]],
nth1(Count,L,List1),
nth1(2,List1,Value),
**NOW I WANT TO PUT THIS Valie in another new list.So finally new list will have 
   New=[18,19,20]**

      

How do I add items to a new list?

+3


source to share


3 answers


see findall / 3 and friends



?- L=[['abc',18],['bcd',19],['def',20]], findall(E,member([_,E],L), R).
L = [[abc, 18], [bcd, 19], [def, 20]],
R = [18, 19, 20].

      

+1


source


The same could be done with a maplist:

?- maplist(nth1(2), [['abc',18],['bcd',19],['def',20]], R).
R = [18, 19, 20].

      



Is the order of the arguments a nth/3

match?

+3


source


Assuming your instructor wants you to come up with a recursive solution yourself, you could just say something like this (given your example data):

slurp( []         , []     ) .
slurp( [[_,X]|Xs] , [X|Ys] ) :- slurp(Xs,Ys) .

      

+1


source







All Articles