How to get all the letters of the alphabet in Elixir?

1

I would like to get all the letters of the alphabet in a string list without having to write letter by letter.

a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

    
asked by anonymous 05.03.2018 / 13:19

1 answer

1

With Elixir you have a way of representing characters by binary values, for example:

Let's say I want to represent the binary value of the letter: a

iex(0)> <<97>>
"a"

The binary varies for the character representation in the upper case:

iex(0)> <<65>>
"A"

Elixir offers a succinct syntax so that binary representations do not get so hard code. You can get the same effect just by putting a ? before the character you want to get the code from:

iex(0)> <<?a>>
"a"
iex(1)> <<?A>>
"A"

With this we have all the tools we need to make our list of alphabet letters:

iex(0)> Enum.map(Enum.to_list(?a..?z), fn(n) -> <<n>> end)

Or if by chance we want the letters in the upper case:

Enum.map(Enum.to_list(?A..?Z), fn(n) -> <<n>> end)
    
05.03.2018 / 13:19