I am trying to do an algorithm to calculate the Ceaser cipher:
I came up with the following code, which in theory should only cipher letters. However, it is not working because of a syntax error I can’t find:
defmodule RotationalCipher do
@doc """
Given a plaintext and amount to shift by, return a rotated string.
Example:
iex> RotationalCipher.rotate("Attack at dawn", 13)
"Nggnpx ng qnja"
"""
@spec rotate(text :: String.t(), shift :: integer) :: String.t()
def rotate(text, shift) do
text
|> String.to_charlist()
|> Enum.map( fn char ->
cond do
is_lower_case?( char ) -> rotate_lower_case( char, shift )
is_upper_case?( char ) -> rotate_upper_case( char, shift )
true -> char
end
end )
|> to_string()
end
def is_lower_case?( char ), do: char >= 97 && char <= 122
def rotate_lower_case( char, shift ), do: 97 + rem( char - 71 + shift, 26 )
def is_upper_case?( char ), do: char >= 65 && char <= 90
def rotate_upper_case ( char, shift ), do: 65 + rem( char - 71 + shift, 26 )
end
Error:
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
** (SyntaxError) rotational_cipher.exs:27: unexpected parentheses. If you are making a function call, do not insert spaces between the function name and the opening parentheses. Syntax error before: ‘(’
(elixir) lib/code.ex:767: Code.require_file/2
What am I doing wrong?