Last Updated: February 25, 2016
· spiegela

Teaching Kids Recursion (in Erlang)

%% In teaching my kids Erlang, I needed a good example for them of
%% Recursion.  Luckily, my kids know several (a thousand?) songs that
%% just repeat over and over. And over. And over.
%% I decided to put the "Frog Song" into code, to give them an example
%% of recursion with a base case.
%% Nothing fancy, but I think they get the idea.



sing(0) ->
  io:format("Ribbit.  Ribbit.~n");
sing(Count) ->
    "~w Green and speckled frogs sat on a speckled log.~n" ++
    "Eating the most delicious bugs.  One jumped into the~n" ++
    "pool, where it was nice and cool, then there were ~w~n" ++
    "green speckled frogs.~n~n",
    [Count, Count - 1]),
  sing(Count - 1).