-record (open_letter_acc, {opened_count = 0, opened = []}).

open_letter(Word, Opened, Letter) ->
  Folder = fun(L, Acc) ->
    if L == Letter ->
      AlreadyOpenedCount = Acc#open_letter_acc.opened_count,
      AlreadyOpened = Acc#open_letter_acc.opened,
      Acc#open_letter_acc{opened_count = AlreadyOpenedCount + 1, opened = AlreadyOpened ++ [L]};
    true ->
      AlreadyOpened = Acc#open_letter_acc.opened,
      Acc#open_letter_acc{opened = AlreadyOpened ++ [<<"_"/utf8>>]}
    end
  end,
  Result = lists:foldl(Folder, #open_letter_acc{opened_count=0, opened=[]}, Word),
  case Result#open_letter_acc.opened_count of
    0 ->
      {error, not_found};
    Count when Count > 0 ->
      if
        Result#open_letter_acc.opened =:= Opened ->
          {error, already_opened};
        true ->
          {ok, {Result#open_letter_acc.opened, Result#open_letter_acc.opened_count}}
      end
  end.

Изменить пасту