How to print alphabet in Elixir? (aa ab ac etc)

So I want to print the alphabet but in this format
aa ab acaz

I was able to write that in JavaScript like this

const alphabet = "abcdefghijklmnopqrstuvwxyz";
for (const a of alphabet.split('').map(b => `a${b}`)) console.log(a);

what methods do I need in Elixir to get the same results?

1 Like
'abcdefghijklmnopqrstuvwxyz' |> Enum.map(&"a#{[&1]}")

there is quite a lot going on here:

3 Likes

whats up with all the {} and [] around {[&1]}? + Can I simply Enum.join behind the map? like |> Enum.join(" ")

Since op wants to print each pair, I would do it like below. Also your use of the charlist is a bit non-idiomatic imo.

alphabet = ~w(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)

Enum.each(alphabet, fn letter ->
  IO.puts("a#{letter}")
end)
4 Likes

yes, here some insight with dbg (needs Elixir 1.14)

'abcdefghijklmnopqrstuvwxyz'
|> Enum.map(&"a#{[&1]}")
|> Enum.join(",")
|> dbg
'abcdefghijklmnopqrstuvwxyz' #=> 'abcdefghijklmnopqrstuvwxyz'
|> Enum.map(&"a#{[&1]}") #=> ["aa", "ab", "ac", "ad", ...]
|> Enum.join(",") #=> "aa,ab,ac,ad,..."
2 Likes

I’m a pirate :pirate_flag:

3 Likes

the &"a#{[&1]}" is a bit confusing but thanks! :smiley:

Its extra-confusing. sorry. Wanted to pack some nice features into that. For a nice solution look at Stefans post.

1 Like

what exactly is “w” here at the first line?

https://hexdocs.pm/elixir/1.14/Kernel.html#sigil_w/2

It’s just a nicer way of writing ["a", "b", "c", ...]

1 Like

FYI, you can use a range for that instead of a string with "abcdefg......." etc. The ? operator gets the codepoint of a character in a char list. From there you can do anything as you would with a list.

?a..?z
2 Likes
alphabet = ?a..?z

like this?

yes.

iex(21)> ?a..?z |> Enum.to_list == 'abcdefghijklmnopqrstuvwxyz'
true
iex(22)> 0..10 |> Enum.to_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

So this should be the most confusing solution to the problem.

iex()> ?a..?z |> Enum.map_join(",", &"a#{[&1]}")
"aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az"
8 Likes

Yeah exactly like that! :sunglasses:

alphabet = ?a..?z

Enum.each(alphabet, fn letter ->
  IO.puts("a#{letter}")
end)

hmm, but when doing this, im getting a weird output
a97
a98
a99
a100
a101
a102
a103
a104

read about charlists.

for glyph <- 97..122, do: <<97, glyph, 10>>
2 Likes

It’s because you’re using a binary instead of a char list. Char lists will display all codepoints in the shell if they represent printable characters, but they’re still stored as numbers. When you convert to string with the interpolation, you’re calling the String.Chars protocol. Basically you’re converting the integer to a string.

that one is nice, but you can do:

for glyph <- ?a..?z, do: <<"a", glyph, "\n">>
1 Like

There are many solutions depending on use case, but the one closest to JavaScript code would be:

alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet |> String.graphemes() |> Enum.map(& "a" <> &1)
# or alternatively:
alphabet |> String.split("", trim: true) |> Enum.map(& "a" <> &1

Helpful resources:

  1. String.graphemes/1
  2. String.split/3
6 Likes