I've been working on the exercises from Dave Thomas's Programming in Elixir. I came across one that says to write a function (called caesar
for some reason) that takes a charlist and an integer to add to each element in the charlist, cycling back around to 'a' if it goes past 'z', so you should be able to call it like
MyList.caesar('ryvke', 13)
and it should return a string.
I have a function that maps over the list and performs the addition, but it returns a charlist, and I can't figure out how to convert it to a string:
defmodule MyList do
def caesar(list, n) do
Enum.map list, &(perform_addition(&1, n))
|> to_charlist
|> to_string
end
defp perform_addition(char_val, n) when char_val < 122 do
char_val + n
end
defp perform_addition(_, n) do
97 + n
end
end
I've tried:
- Reading the docs on binaries, strings, and charlists
- Reading about charlists on ElixirSchool
- Trying to convert it from a charlist to a binary and then to a string after following this SO question
The attempt based on the last bullet led to:
MyList.caesar('ryvke', 13)
# => <<127, 194, 134, 194, 131, 120, 114>>