annotate 5-Erlang/day2.erl @ 87:2b5341fc4555

Add Haskell Day 1 code and notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Jun 2019 17:31:22 +0100
parents 41c7cd909218
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
74
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 -module(day2).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 -export([find_tuple/2]).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 -export([calc_prices/1]).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 -export([score_tictactoe/1]).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 -export([has_won/2]).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 % Do a more Erlang-esque "crash if not matched" behaviour rather than an atom of "undefined" or "null"
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 find_tuple([{Key, Value}|_], Key) -> Value;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 find_tuple([{_,_}|Tail], Key) -> find_tuple(Tail, Key).
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 calc_prices(ProductList) -> [{Item, Quantity * Price} || {Item, Quantity, Price} <- ProductList].
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 has_won(Board, P) ->
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 case Board of
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 { P, P, P,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 _, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 _, _, _ } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 { _, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 P, P, P,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 _, _, _ } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 { _, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 _, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 P, P, P } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 { P, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 P, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 P, _, _ } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 { _, P, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 _, P, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 _, P, _ } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 { _, _, P,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 _, _, P,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 _, _, P } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 { P, _, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 _, P, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 _, _, P } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 { _, _, P,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 _, P, _,
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 P, _, _ } -> true;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 _ -> false
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 end.
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 score_tictactoe(Board) ->
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 State = { has_won(Board, x), has_won(Board, o), lists:any(fun(Val) -> Val == b end, tuple_to_list(Board)) },
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 case State of
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 { true, false, _ } -> x;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 { false, true, _ } -> o;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 { _, _, true } -> no_winner;
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 { false, false, false } -> cat
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 % And { true, true, false } exceptions as it is invalid
41c7cd909218 Write up exercises for Erlang day 2 (including bonus Tic Tac Toe)
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 end.