Get a list of numbers from a list of complex terms

I have a list of complex terms with the same functor and arity, but different arguments. Something like that:

Elements = [element(a, 1), element(b,2), element(c,3)]

And from it I want to create a new list containing only two arguments of each complex term in the list, for example:

Numbers = [1,2,3]

Is there a way to develop a predicate to solve this problem for any input list length?

+4
source share
1 answer

Timothy answers with findall/3:findall(X,member(element(_,X), Elements),Numbers).

Solution with recursive predicate:

element_indexes([], []).
element_indexes([element(_,N)|Es],[N|Ns]) :-
    element_indexes(Es, Ns).  
+1
source

All Articles