Skip to content

Latest commit

 

History

History
552 lines (451 loc) · 9.53 KB

appendix-a.asciidoc

File metadata and controls

552 lines (451 loc) · 9.53 KB

Appendix A: Solutions to Études

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.

Solution 2-1

Here is a suggested solution for Étude 2-1.

geom.erl

link:code/ch02-01/geom.erl[role=include]

Solution 2-2

Here is a suggested solution for Étude 2-2.

geom.erl

link:code/ch02-02/geom.erl[role=include]

Solution 2-3

Here is a suggested solution for Étude 2-3.

geom.erl

link:code/ch02-03/geom.erl[role=include]

Solution 3-1

Here is a suggested solution for Étude 3-1.

geom.erl

link:code/ch03-01/geom.erl[role=include]

Solution 3-2

Here is a suggested solution for Étude 3-2.

geom.erl

link:code/ch03-02/geom.erl[role=include]

Solution 3-3

Here is a suggested solution for Étude 3-3.

geom.erl

link:code/ch03-03/geom.erl[role=include]

Solution 3-4

Here is a suggested solution for Étude 3-4.

geom.erl

link:code/ch03-04/geom.erl[role=include]

Solution 4-1

Here is a suggested solution for Étude 4-1.

geom.erl

link:code/ch04-01/geom.erl[role=include]

Solution 4-2

Here is a suggested solution for Étude 4-2.

dijkstra.erl

link:code/ch04-02/dijkstra.erl[role=include]

Solution 4-2

Here is another solution for Étude 4-2. This solution uses guards instead of if.

dijkstra.erl

link:code/ch04-02b/dijkstra.erl[role=include]

Solution 4-3

Here is a suggested solution for Étude 4-3.

powers.erl

link:code/ch04-03/powers.erl[role=include]

powers_traced.erl

This code contains output that lets you see the progress of the recursion.

link:code/ch04-03/powers_traced.erl[role=include]

Solution 4-4

Here is a suggested solution for Étude 4-4.

powers.erl

link:code/ch04-04/powers.erl[role=include]

powers_traced.erl

This code contains output that lets you see the progress of the recursion.

link:code/ch04-04/powers_traced.erl[role=include]

Solution 4-5

Here is a suggested solution for Étude 4-5.

powers.erl

link:code/ch04-05/powers.erl[role=include]

Solution 5-1

Here is a suggested solution for Étude 5-1.

geom.erl

link:code/ch05-01/geom.erl[role=include]

ask_area.erl

link:code/ch05-01/ask_area.erl[role=include]

Solution 5-2

Here is a suggested solution for Étude 5-2.

dates.erl

link:code/ch05-02/dates.erl[role=include]

Solution 6-1

Here is a suggested solution for Étude 6-1.

stats.erl

link:code/ch06-01/stats.erl[role=include]

Solution 6-2

Here is a suggested solution for Étude 6-2.

stats.erl

link:code/ch06-02/stats.erl[role=include]

Solution 6-3

Here is a suggested solution for Étude 6-3 with leap years handled in the julian/5 function.

dates.erl

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.

dates.erl

link:code/ch06-03b/dates.erl[role=include]

Solution 6-4

Here is a suggested solution for Étude 6-4.

teeth.erl

link:code/ch06-04/teeth.erl[role=include]

stats.erl

link:code/ch06-04/stats.erl[role=include]

Solution 6-5

Here is a suggested solution for Étude 6-5.

non_fp.erl

link:code/ch06-05/non_fp.erl[role=include]

Solution 7-1

Here is a suggested solution for Étude 7-1.

calculus.erl

link:code/ch07-01/calculus.erl[role=include]

Solution 7-2

Here is a suggested solution for Étude 7-2.

patmatch.erl

link:code/ch07-02/patmatch.erl[role=include]

Solution 7-3

Here is a suggested solution for Étude 7-3.

stats.erl

link:code/ch07-03/stats.erl[role=include]

Solution 7-4

Here is a suggested solution for Étude 7-4.

dates.erl

link:code/ch07-04/dates.erl[role=include]

Solution 7-5

Here is a suggested solution for Étude 7-5.

cards.erl

link:code/ch07-05/cards.erl[role=include]

Solution 7-6

Here is a suggested solution for Étude 7-6.

cards.erl

link:code/ch07-06/cards.erl[role=include]

Solution 8-1

Here is a suggested solution for Étude 8-1.

cards.erl

link:code/ch08-01/cards.erl[role=include]

game.erl

link:code/ch08-01/game.erl[role=include]

Solution 9-1

Here is a suggested solution for Étude 9-1.

stats.erl

link:code/ch09-01/stats.erl[role=include]

Solution 9-2

Here is a suggested solution for Étude 9-2.

bank.erl

link:code/ch09-02/bank.erl[role=include]

Solution 10-1

Here is a suggested solution for Étude 10-1.

phone_records.hrl

link:code/ch10-01/phone_records.hrl[role=include]

phone_ets.erl

link:code/ch10-01/phone_ets.erl[role=include]

generate_calls.erl

link:code/ch10-01/generate_calls.erl[role=include]

Solution 10-2

Here is a suggested solution for Étude 10-2.

phone_records.hrl

link:code/ch10-02/phone_records.hrl[role=include]

phone_mnesia.erl

link:code/ch10-02/phone_mnesia.erl[role=include]

pet_records.hrl

link:code/ch10-02/pet_records.hrl[role=include]

pet_mnesia.erl

link:code/ch10-02/pet_mnesia.erl[role=include]

Solution 11-1

Here is a suggested solution for Étude 11-1.

weather.erl

link:code/ch11-01/weather.erl[role=include]

weather_sup.erl

link:code/ch11-01/weather_sup.erl[role=include]

Solution 11-2

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.

weather.erl

-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, "").

Solution 11-3

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}.

Solution 11-4

Here is a suggested solution for Étude 11-4.

chatroom.erl

link:code/ch11-04/chatroom.erl[role=include]

person.erl

link:code/ch11-04/person.erl[role=include]