Hi there Elixir guys and gals,
I’m new to Elixir, trying to build a name calculator for exercise.
It should let you type in a name and give you back a number! So, “a” is represented by 1, “b” by 2, but it only counts up to 9 so that “j” is also represented by 1, ending with “z” as 8. Those numbers of the respective letters will be added and the result is the response the user should get.
So I tried it like that:
defmodule NameCalc do
def add(x) do
letterlist = String.graphemes("#{x}")
for n <- letterlist, do: %{"a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "f" => 6, "g" => 7,
"h" => 8, "i" => 9, "j" => 1, "k" => 2, "l" => 3, "m" => 4, "n" => 5,
"o" => 6, "p" => 7, "q" => 8, "r" => 9, "s" => 1, "t" => 2, "u" => 3,
"v" => 4, "w" => 5, "x" => 6, "y" => 7, "z" => 8}[n]
|> Sum.sum
end
end
defmodule Sum do
def sum ([]) do
0
end
def sum([h | t]) do
h + sum(t)
end
end
It seems like I’m doing something (several things) completely wrong!
It yells at me:
undefined function x/0
I’m sure this isn’t the only mistake in my file?
Where are my mistakes? Thx everybody.
V. Jim Deeno