vermaxik

vermaxik

List Comparison (how it works)

Hello Elixirforum,

Could you please explain how List Comparison works in Elixir?

Why such list of integers returns true

iex(15)> [3,10] > [3,3,3,3]
true

And the same example list of strings:

iex(16)> ["3","10"] > ["3", "3", "3", "3"]
false

I would expect in both examples false because of list size.

Thank you!

Marked As Solved

NobbZ

NobbZ

List comparison works by comparing element by element.

For each pair of equal elements, the comparison advances to the next. The first that is not equal decides. And 10 is greater than 3 while "10" is not greater than "3".

You can learn more in the docs:

Also Liked

LostKobrakai

LostKobrakai

Lists are compared element by element, disregarding the size (tuples are compared by size then elements). So it should come down to integers and binaries comparing differently.

iex(1)> "10" > "3"
false
iex(2)> 10 > 3
true
hauleth

hauleth

As for reasoning for this behaviour - computing size of list is as expensive as traversing whole list, so comparing two lists first by length and then by elements would make whole operation more expensive.

Last Post!

arbaz_asif

arbaz_asif

The most efficient way to compare two lists on the basis of their sizes is by using Enum.count() function from Enum Module. It will work like this:

Enum.count([3,10]) > Enum.count([3,3,3,3])
false

Enum.count(["3","10"]) > Enum.count(["3", "3", "3", "3"])
false

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement