How to get all letters of the alphabet in Elixir?

Asked

Viewed 67 times

2

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"]

1 answer

2

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"

Binary varies for high-box character representation:

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

Elixir offers a sugar syntax so that binary representations don’t get so hard code. You can get the same effect just by putting one ? before the character from which you want to obtain the code:

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

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

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

Or if by some chance we want the upper-case letters:

Enum.map(Enum.to_list(?A..?Z), fn(n) -> <<n>> end)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.