Solution in Erlang