Skip to content

Latest commit

 

History

History

ListsAndRecursion-3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Your turn : ListsAndRecursions-3

An Elixir single-quoted string is actually a list of individual character codes.

Write a caesar(list, n) function that adds n to each list element, wrapping if the addition results in a character greater than z.

My implementation

iex(1)> defmodule MyList do
...(1)>   def caesar([], _n) do
...(1)>     []
...(1)>   end
...(1)> 
...(1)>   def caesar([head | tail], n) when head + n <= ?z do
...(1)>     [head + n | caesar(tail, n)]
...(1)>   end
...(1)> 
...(1)>   def caesar([head | tail], n) do
...(1)>     [head + n - 26 | caesar(tail, n)]
...(1)>   end
...(1)> end
{:module, MyList,
 <<70, 79, 82, 49, 0, 0, 5, 184, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 166, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
 {:caesar, 2}}
 
iex(2)> MyList.caesar('ryvkve', 13)
'elixir'