Praveen Kumar
Praveen Kumar

Reputation: 161

How to Perform list operation concept in Elixir?

In Ruby list concept I try to subtract:

["a","b","a","c","c"]--["a","c"] = ["b"]

But same i tried in Elixir it return ["a","c","b"], in my knowledge mathematically its returns ["b","c"]

Why elixir behave like that?

Upvotes: 0

Views: 150

Answers (1)

Onorio Catenacci
Onorio Catenacci

Reputation: 15293

1.) Elixir isn't Ruby. I can't stress that enough. While it shares syntax, forget your expectations that it should behave the same as Ruby does. By the way, I don't know Ruby but the result you're getting in Ruby seems somewhat odd.

2.) When I try what you asked from iex this is what I get:

Interactive Elixir (1.1.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> l1 = ["a","b","a","c","c"]
["a", "b", "a", "c", "c"]
iex(2)> l2 = ["a","c"]
["a", "c"]
iex(3)> l3 = l1 -- l2
["b", "a", "c"]
iex(4)>

This is the behavior that I would have expected. That is, it got rid of one of the "a"'s and one of the "c"'s. If you're getting a different result then you need to share more details with us: version of Elixir, OS (with version) etc. etc. etc.

Upvotes: 2

Related Questions