Last Updated: February 25, 2016
·
450
· 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.

-module(frogs_song).

-export([sing/1]).

sing(0) ->
  io:format("Ribbit.  Ribbit.~n");
sing(Count) ->
  io:format(
    "~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).