Home > Enterprise >  Getting specific part of list elemnt
Getting specific part of list elemnt

Time:06-11

I have this list [c1=2*3,c2=5*1,c3=1*1] and i want to create a new list with just the 3rd element of each element of the list, so something like this [2,5,1]. I'm using nth0 to access each of the elements of the list but from there i can't progress much further. Thank you in advance.

CodePudding user response:

From your example, I assume that the list is composed by elements of the form (A = B * C) and that you want to filter only the component B of each one of them. In this case, a possible solution is as follows:

filter([], []).
filter([(_ = B * _)|Xs], [B|Ys]) :- filter(Xs, Ys).

Example:

?- filter([c1=2*3, c2=5*1, c3=1*1], L).
L = [2, 5, 1].
  • Related