On the contrary, I'm very interested in Erlang. One of the exercises is to tell in which state a Tic-Tac-Toe board is. Did X win ? Did O win ? No winner yet ? No more moves ? This is the solution I came up with, after several refactoring passes. I like the way we can interact with lists and tuples.
-module(tic).
-export([winner/1]).
winner(Board) ->
[A,B,C,D,E,F,G,H,I] = Board,
% Filter the board for winning combinations
Result = lists:filter(
fun({X,Y,Z}) -> (X=:=Y) and (X=:=Z) end,
[{A,B,C},{D,E,F},{G,H,I},{A,D,G},{B,E,H},{C,F,I},{A,E,I},{C,E,G}]),
% Checking the winner
case Result of
% There is a winner
[First|_] -> {Winner,_,_} = First, Winner;
% No winner
_ ->
% Is there any slot left to play ?
case lists:any(fun(X) -> X=:="" end, Board) of
true -> "no winner";
false -> "cat"
end
end.
Seven languages in Seven weeks is an ambitious reading list. I juggle a lot of parallel priorities at work too, and finding the right tools makes all the difference. Proper infrastructure planning, like ensuring reliable HVAC systems in server rooms, is just as important as choosing the right programming language.
ReplyDelete