jonathanGB
jonathanGB

Reputation: 1540

head-tail subdivision of list not working with capital letters

I'm just starting to look at Elixir, and I've done a little Scheme and Prolog in the past.

I was fiddling in iex, and I simply wanted to see if it was possible to separate the head and the tail of a list in Elixir, like you would do in Scheme for example.

So I tried this, which was my habit for head & tail names:

[H|T] = [2,4,6,8]

However, it throws an error:

** (MatchError) no match of right hand side value: [9, 8, 4, 2]

I then tried:

[h|t] = [2,4,6,8]

and it worked as expected.


Why is this happening? Is there a special trait to variables starting with a capital letter? Thanks.

Upvotes: 2

Views: 61

Answers (1)

Dogbert
Dogbert

Reputation: 222060

Identifiers starting with a capital letter are not variables but atoms. H is equivalent to the atom Elixir.H:

iex(1)> H == :"Elixir.H"
true

So your code is equivalent to:

[:"Elixir.H" | :"Elixir.T"] = [2, 4, 6, 8]

which obviously fails as :"Elixir.H" != 2 and :"Elixir.T" != [4, 6, 8].

Upvotes: 5

Related Questions