今度の練習問題はカエサル暗号だ。
defmodule MyList do def map([], _func), do: [] def map([head|tail], func), do: [func.(head) | map(tail, func)] def reduce([], value, _func) do value end def reduce([head|tail], value, func) do reduce(tail, func.(head, value), func) end def caesar(list, n) do map(list, fn c -> rem(c + n - ?a, 26) + ?a end) end end
^o^ > iex practice_7_3.exs Eshell V8.0 (abort with ^G) Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> MyList.caesar('ryvkve', 13) 'elixir'