Skip to content

Latest commit

 

History

History
620 lines (505 loc) · 10.2 KB

appendix-a.asciidoc

File metadata and controls

620 lines (505 loc) · 10.2 KB

Appendix A: Solutions to Études

Here are the solutions that I came up with for the études in this book.

Solution 2-1

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

geom.ex

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

Solution 2-2

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

geom.ex

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

Solution 2-3

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

geom.ex

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

Solution 3-1

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

geom.ex

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

Solution 3-2

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

geom.ex

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

Solution 3-3

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

geom.ex

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

Solution 3-4

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

geom.ex

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

Solution 4-1

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

geom.ex

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

Solution 4-2

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

dijkstra.ex

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

Solution 4-2

Here is another solution for Étude 4-2. This solution uses multiple clauses with guards instead of cond.

dijkstra.ex

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

Solution 4-3

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

powers.ex

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

powers_traced.ex

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

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

Solution 4-4

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

powers.ex

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

Solution 4-5

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

powers.ex

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

Solution 5-1

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

geom.ex

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

ask_area.ex

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

Solution 5-2

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

The geom.ex file is the same as in Étude 5-1.

ask_area.ex

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

Solution 5-3

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

dates.ex

link:code/ch05-03/dates.ex[role=include]

Solution 6-1

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

stats.ex

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

Solution 6-2

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

dates.ex

link:code/ch06-02/dates.ex[role=include]

Here is a suggested solution for Étude 6-3 with leap years handled in a different way.

dates.ex

link:code/ch06-02b/dates.ex[role=include]

Solution 6-3

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

teeth.ex

link:code/ch06-03/teeth.ex[role=include]

stats.ex

link:code/ch06-03/stats.ex[role=include]

Solution 6-4

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

non_fp.ex

link:code/ch06-04/non_fp.ex[role=include]

Solution 7-1

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

college.ex

link:code/ch07-01/college.ex[role=include]

Solution 7-1

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

geography.ex

link:code/ch07-02/geography.ex[role=include]

Solution 7-3

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

geography.ex

link:code/ch07-03/geography.ex[role=include]

Solution 7-4

Here is a suggested solution for Étude 7-4. The code shown here is the code required to implement the additional protocols; the remaining code is the same as in

city.ex

link:code/ch07-04/city.ex[role=include]

Solution 8-1

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

calculus.ex

link:code/ch08-01/calculus.ex[role=include]

Solution 8-2

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

list_comp.ex

link:code/ch08-02/list_comp.ex[role=include]

Solution 8-3

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

stats.ex

link:code/ch08-03/stats.ex[role=include]

Solution 8-4

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

dates.ex

link:code/ch08-04/dates.ex[role=include]

Solution 8-5

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

cards.ex

link:code/ch08-05/cards.ex[role=include]

Solution 8-6

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

cards.ex

link:code/ch08-06/cards.ex[role=include]

Solution 9-1

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

cards.ex

link:code/ch09-01/cards.ex[role=include]

game.ex

link:code/ch09-01/game.ex[role=include]

player.ex

link:code/ch09-01/player.ex[role=include]

Solution 10-1

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

stats.ex

link:code/ch10-01/stats.ex[role=include]

Solution 10-2

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

bank.ex

link:code/ch10-02/bank.ex[role=include]

Solution 11-1

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

phone_ets.ex

link:code/ch11-01/phone_ets.ex[role=include]

generate_calls.ex

This is the program I used to generate the list of phone calls.

link:code/ch11-01/generate_calls.ex[role=include]

Solution 12-1

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

weather.ex

link:code/ch12-01/weather.ex[role=include]

weather_sup.ex

link:code/ch12-01/weather_sup.ex[role=include]

Solution 12-2

Here is a suggested solution for Étude 12-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.ex

def report(station) do
  :gen_server.call(Weather, station)
end

def recent() do
  :gen_server.cast(__MODULE__, "")
end

Solution 12-3

Here is a suggested solution for Étude 12-3. Since the bulk of the code is identical to the previous étude, the only code shown here is the added and revised code.

# convenience method for startup
def start_link do
  :gen_server.start_link({:global, __MODULE__}, __MODULE__, [], [])
end

def connect(other_node) do
  case :net_adm.ping(other_node) do
    :pong ->
      IO.puts("Connected to server.")
      :ok
    :pang ->
      IO.puts("Could not connect.")
      :error
  end
end

def report(station) do
  :gen_server.call({:global, __MODULE__}, station)
end

def recent() do
  result = :gen_server.call({:global, __MODULE__}, :recent)
  IO.puts("Recently visited: #{inspect(result)}")
end

Solution 12-4

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

chatroom.ex

link:code/ch12-04/chatroom.ex[role=include]

person.ex

link:code/ch12-04/person.ex[role=include]

Solution 13-1

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

atomic_maker.ex

link:code/ch13-01/atomic_maker.ex[role=include]

atomic.ex

link:code/ch13-01/atomic.ex[role=include]

Solution 13-2

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

duration.ex

link:code/ch13-02/duration.ex[role=include]

Solution 13-3

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

duration.ex

link:code/ch13-03/duration.ex[role=include]