Erlang List Generation

I have 2 lists:

["asd", "dsa"].

[[123, "asd"], [4534, "fgh"]].

How can I generate the following list: Ined list, that tail of each nested list =: = another item from 1 list.

In this example:

["asd", "dsa"].

[[123, "asd"], [4534, "fgh"]].

"asd" =: = "asd" →

Output List:

[123, "asd"] 

I'm trying to:

Here S = [[123, "asd"], [4534, "fgh"]]. D = ["asd", "dsa"].

List = lists: filter (fun (X) → lists: last (X) =: = D end, S),

But D is in this list of examples, and I need a list item.

How to do it?

+3
source share
2 answers

Maybe something like:

1> [X || X<-[[1,2,4],[7,8,3],[2,5,4],[9,1,6]], Y<-[4,3], lists:last(X)=:=Y].    
[[1,2,4],[7,8,3],[2,5,4]]

Or using your sample data:

2> [X || X<-[[123,"asd"], [4534,"fgh"]], Y<-["asd","dsa"], lists:last(X)=:=Y].
[[123,"asd"]]
+3
source

A slightly more direct way to write:

lists:filter(fun (X) -> lists:member(lists:last(X), D) end, S).

or with a list:

[ X || X <- S, lists:member(lists:last(X), D) ].

, D, . D .

+2

Source: https://habr.com/ru/post/1791642/


All Articles