Relative vs Absolute If someone facing north at point A in this grid wanted directions to follow the green path (as they can only follow gridlines) to point B you might tell them:  

Go North, North, West, East, East, South, East, East.

or equivalently

Go Forward, Forward, Left, Back, Forward, Right, Left, Forward.
(Where a command of Right, Left, or Back implicitly means turn in that direction, then go forward.)



Write a function with one argument that translates between these absolute and relative directions along the same path, not just to the same point. Assume the directed person always starts facing north.

If the argument is a string of the letters NSEW, return the equivalent relative directions.
e.g. f("NNWEESEE") returns the string FFLBFRLF.

If the argument is a string of the letters FBLR, return the equivalent absolute directions.
e.g. f("FFLBFRLF") returns the string NNWEESEE.

The empty string yields itself. Assume no other input cases.

If your language does not have functions or strings use whatever seems most appropriate.

The shortest code in bytes wins.
CJam, 57 53 49

{"NESW""FRBL"_3$&amp;!!:Q$:R^^f#0\{{+0\}Q*_@-R=\}%);}


Previous version

{"NESW""FRBL"_3$0=#W&gt;:Q{\}*:R;f#0\{{+0\}Q*_@-R=\}%);}


Example:

{"NESW""FRBL"_3$0=#W&gt;:Q{\}*:R;f#0\{{+0\}Q*_@-R=\}%);}:T;
"NNWEESEE"T
N
"FFLBFRLF"T


Output:

FFLBFRLF
NNWEESEE


How it works

{
  "NESW""FRBL"             " Push the two strings. ";
  _3$0=#W&gt;                 " Check if the first character is in FRBL. ";
  :Q                       " Assign the result to Q. ";
  {\}*                     " Swap the two strings if true. ";
  :R;                      " Assign the top string to R and discard it. ";
  f#                       " Find each character of the input in the string. ";
  0\                       " Push a 0 under the top of the stack. ";
  {                        " For each item (index of character): ";
    {                      " If Q: ";
      +0\                  " A B -&gt; 0 (A+B) ";
    }Q*
    _@-                    " C D -&gt; D (D-C) ";
    R=                     " E -&gt; E-th character in R ";
    \                      " Swap the top two items. ";
  }%
  );                       " Discard the last item in the list. ";
}