Here are the solutions that I came up with for the études in this book. Since I was learning Erlang as I wrote them, you may expect some of the code to be naïve in the extreme.
Here is a suggested solution for Étude 2-1.
Here is a suggested solution for Étude 2-2.
Here is a suggested solution for Étude 2-3.
Here is a suggested solution for Étude 3-1.
Here is a suggested solution for Étude 3-2.
Here is a suggested solution for Étude 3-3.
Here is a suggested solution for Étude 3-4.
Here is a suggested solution for Étude 4-1.
Here is a suggested solution for Étude 4-2.
Here is another solution for Étude 4-2. This solution uses guards instead of if.
Here is a suggested solution for Étude 4-3.
Here is a suggested solution for Étude 4-4.
Here is a suggested solution for Étude 4-5.
Here is a suggested solution for Étude 5-1.
Here is a suggested solution for Étude 5-2.
Here is a suggested solution for Étude 6-1.
Here is a suggested solution for Étude 6-2.
Here is a suggested solution for Étude 6-3 with leap years handled in the julian/5 function.
link:code/ch06-03/dates.erl[role=include]
Here is a suggested solution for Étude 6-3 with leap years handled in the julian/1 function.
Here is a suggested solution for Étude 6-4.
Here is a suggested solution for Étude 6-5.
Here is a suggested solution for Étude 7-1.
Here is a suggested solution for Étude 7-2.
Here is a suggested solution for Étude 7-3.
Here is a suggested solution for Étude 7-4.
Here is a suggested solution for Étude 7-5.
Here is a suggested solution for Étude 7-6.
Here is a suggested solution for Étude 8-1.
Here is a suggested solution for Étude 9-1.
Here is a suggested solution for Étude 9-2.
Here is a suggested solution for Étude 10-1.
Here is a suggested solution for Étude 10-2.
Here is a suggested solution for Étude 11-1.
Here is a suggested solution for Étude 11-2. Since the bulk of the code is identical to the code in the previous étude, the only code shown here is the revised -export list and the added functions.
-export([report/1, recent/0]). % wrapper functions
%% Wrapper to hide internal details when getting a weather report
report(Station) ->
gen_server:call(?SERVER, Station).
%% Wrapper to hide internal details when getting a list of recently used
%% stations.
recent() ->
gen_server:cast(?SERVER, "").
Here is a suggested solution for Étude 11-3. Since the bulk of the code is identical to the previous étude, the only code shown here is the added and revised code.
%% @doc Connect to a named server
connect(ServerName) ->
Result = net_adm:ping(ServerName),
case Result of
pong -> io:format("Connected to server.~n");
pang -> io:format("Cannot connect to ~p.~n", [ServerName])
end.
%% Wrapper to hide internal details when getting a weather report
report(Station) ->
gen_server:call({global, weather}, Station).
%% Wrapper to hide internal details when getting a list of recently used
%% stations.
recent() ->
gen_server:call({global,weather}, recent).
%%% convenience method for startup
start_link() ->
gen_server:start_link({global, ?SERVER}, ?MODULE, [], []).
%%% gen_server callbacks
init([]) ->
inets:start(),
{ok, []}.
handle_call(recent, _From, State) ->
{reply, State, State};
handle_call(Request, _From, State) ->
{Reply, NewState} = get_weather(Request, State),
{reply, Reply, NewState}.
handle_cast(_Message, State) ->
io:format("Most recent requests: ~p\n", [State]),
{noreply, State}.
Here is a suggested solution for Étude 11-4.