## Dustin’s Programming Problem Take One

My colleague Matt Ryall wrote about this simple algorithm for marking up a series of letters — which is complex enough to be interesting.

I wrote a version in CAL:

``````arr = ['a', 'b', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e',
'f', 'e', 'f', 'e', 'f', 'a', 'a', 'a', 'f', 'f', 'f'];

data State = State string :: String current :: (Maybe Char) count :: Int;

out =
let
initial = State "" Nothing 0;
f :: State -> Char -> State;
f state char =
let
State s current count = state;
charStr = fromChar char;
appendSameChar =
if count == 2 then
s ++ " <span>" ++ charStr
else
s ++ " " ++ charStr;
appendDiffChar =
if count > 2 then
s ++ "</span> " ++ charStr
else
s ++ " " ++ charStr;
in
case current of
Nothing -> State (fromChar char) (Just char) (1 :: Int);
Just c ->
if c == char then
State appendSameChar (Just char) (count+1)
else
State appendDiffChar (Just char) 1;
;
finalState = foldLeftStrict f initial arr;
in
finalState.State.string ++(if finalState.State.count > 2 then
"</span>"
else
"");
``````

Positive proof that you can write verbose, confusing code in any language — but at least this code had no bugs — once it compiled it worked correctly first time.

As a follow up to this I will try to do better!