Charles Okwuagwu
Charles Okwuagwu

Reputation: 10866

is there a Built-in function to check if one list is contained in another? in ELIXIR or ERLANG

Without using brute-force (i.e. checking element by element), is there a built-in function that can check if one list is contained within another?

Upvotes: 3

Views: 1229

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

I am not sure if it is a brute-force, and it has some limitations, but one might use Kernel.--/2

{l1, l2} = {[1,2,3], [1,2,3,4]}
l1 -- l2
#⇒ []
l2 -- l1
#⇒ [4]

The exact requested checker would be:

Enum.count(contained -- container) == 0

Or, as noted in comments by @Hynek-Pichi-Vychodil

contained -- container == []

Upvotes: 10

Related Questions