Question

Basically I have two lists something like

L1 = [one, two, three, five, eleven, million]
L2 = [five, million]

so I want to filter the elements from the second list L2

to get

[one, two, tree, eleven]

I have use the foldl function to loop the L1 and then a foreach loop to decide the element to append comparing from the list 2 but i do not seem to get a logic right: I have something like this

56 filter_a(L1,L2) ->
 57    List = foldl(fun(X,A) ->
 58                 L = lists:foreach(fun(E) ->
 59                             case E =:= X of
 60                                 true ->
 61                                     [];
 62                                 _-> 
 63                                     X
 64                             end 
 65                     end, L2),
 66                 lists:append([A,[L]])
 67         end, [], L1),
 68    List.

How can i do this in an easy way?

Was it helpful?

Solution 2

You can use a list comprehension:

filter_a(L1,L2) ->
    [ X || X <- L1, lists:member(X,L2) =/= true ]. 

OTHER TIPS

Why not just L1 -- L2? The complexity is the same O(N*M).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top