~statianzo/sevenlangs

c1ca0d5093c035aac6fcd1323f054151ce5144ad — Jason Staten 1 year, 10 months ago e3b853e
more erlang exercises
3 files changed, 45 insertions(+), 0 deletions(-)

A erlang/lookup.erl
A erlang/shopping.erl
A erlang/tictactoe.erl
A erlang/lookup.erl => erlang/lookup.erl +11 -0
@@ 0,0 1,11 @@
-module(lookup).

-export([by_key/2]).

by_key(_, []) -> "unknown";
by_key(Key, [{Key, Val} | _]) -> Val;
by_key(Key, [_ | Tail]) -> by_key(Key, Tail).

% lookup:by_key(birthday, [{ice, "cream"}, {birthday, "cake"}]).
% equivalent to proplists:get_value/2_


A erlang/shopping.erl => erlang/shopping.erl +13 -0
@@ 0,0 1,13 @@
-module(shopping).

-export([totals/0]).

% constantize items/0
% https://stackoverflow.com/a/10863003/88340
-compile({inline, [{items, 0}]}).

items() -> [{apple, 5, 2}, {banana, 10, 3}].

totals() ->
    [{Key, Quantity * Price}
     || {Key, Quantity, Price} <- items()].

A erlang/tictactoe.erl => erlang/tictactoe.erl +21 -0
@@ 0,0 1,21 @@
-module(tictactoe).

-export([winner/1]).

is_full(Board) ->
    lists:all(fun (A) -> A == x orelse A == o end,
	      tuple_to_list(Board)).

winner({A, A, A, _, _, _, _, _, _}) -> A;
winner({_, _, _, A, A, A, _, _, _}) -> A;
winner({_, _, _, _, _, _, A, A, A}) -> A;
winner({A, _, _, A, _, _, A, _, _}) -> A;
winner({_, A, _, _, A, _, _, A, _}) -> A;
winner({_, _, A, _, _, A, _, _, A}) -> A;
winner({A, _, _, _, A, _, _, _, A}) -> A;
winner({_, _, A, _, A, _, A, _, _}) -> A;
winner(Board) ->
    case is_full(Board) of
      true -> cat;
      false -> no_winner
    end.