title: Understanding and using foreach in Erlang
body: <p>I have an assignment to complete a caesar cypher in 7 languages.  I'm working on completing it  Erlang currently. I've been exposed to functional languages before so I generally understand what I need to do.  I'm specifically having trouble understanding the usage of the foreach function in Erlang.  I know it's used when you are interested in a side effect, so I'm pretty sure it's the "right" way to do what I want.  I've read <a href="https://stackoverflow.com/questions/14443059/iterate-through-each-file-in-a-directory/14449469#14449469">this answer</a> and the definition of foreach in the Erlang language reference <a href="http://www.erlang.org/doc/man/lists.html#foreach-2" rel="nofollow noreferrer">here</a>.  However, I'm still a little confused and having trouble getting the syntax right.</p>
  
  <pre><code>-module(caesar).
  -export([main/2]).  
  
  enc(Char,Key) when (Char &gt;= $A) and (Char =&lt; $Z) or
                 (Char &gt;= $a) and (Char =&lt; $z) -&gt; 
  Offset = $A + Char band 32, N = Char - Offset,
  Offset + (N + Key) rem 26;
  
  enc(Char, _Key) -&gt;  Char.
  
  encMsg(Msg, Key) -&gt;
     lists:map(fun(Char) -&gt; enc(Char, Key) end, Msg).
  
  main(Message, Key) -&gt; 
  
  Encode = (Key), 
  Decode = (-Key),
  Range = lists:seq(1,26),
  
  io:format("Message: : ~s~n", [Message]),
  Encrypted = encMsg(Message, Encode),
  Decrypted = encMsg(Encrypted, Decode),
  
  io:format("Encrypted:  ~s~n", [Encrypted]), 
  io:format("Decrypted: ~s~n", [Decrypted]),
  io:format("Solution: ").
      %% Foreach belongs here, should execute Encrypted = encMsg(Message, N) where
      %% N is the value in Range for each value in the list, and then print Encrypted.
  </code></pre>