Skip to content

Instantly share code, notes, and snippets.

@MrJaba
Created February 8, 2011 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MrJaba/816838 to your computer and use it in GitHub Desktop.
Save MrJaba/816838 to your computer and use it in GitHub Desktop.
Erlang Day 2 Exercises
-module(daytwo).
-export([find/2]).
-export([shopping/1]).
-export([ttt/1]).
%Given a list of tuples and a key return the value of the key
%[{thing, "thing value"}, {thing_two, "thing_two value"}]
find([], Term) -> [];
find([{Term,Value}|Tail], Term) -> Value;
find([Head|Tail], Term) -> find(Tail, Term).
%List comprehension to calculate the total price given a list of tuples
shopping(List) -> [{Product, Quantity * Price} || {Product, Quantity, Price} <- List].
%Tic Tac Toe game
ttt([x,x,x,_,_,_,_,_,_]) -> x;
ttt([_,_,_,x,x,x,_,_,_]) -> x;
ttt([_,_,_,_,_,_,x,x,x]) -> x;
ttt([x,_,_,_,x,_,_,_,x]) -> x;
ttt([_,_,x,_,x,_,x,_,_]) -> x;
ttt([x,_,_,x,_,_,x,_,_]) -> x;
ttt([_,x,_,_,x,_,_,x,_]) -> x;
ttt([_,_,x,_,_,x,_,_,x]) -> x;
ttt([o,o,o,_,_,_,_,_,_]) -> o;
ttt([_,_,_,o,o,o,_,_,_]) -> o;
ttt([_,_,_,_,_,_,o,o,o]) -> o;
ttt([o,_,_,_,o,_,_,_,o]) -> o;
ttt([_,_,o,_,o,_,o,_,_]) -> o;
ttt([o,_,_,o,_,_,o,_,_]) -> o;
ttt([_,o,_,_,o,_,_,o,_]) -> o;
ttt([_,_,o,_,_,o,_,_,o]) -> o;
ttt([_,_,_,_,_,_,_,_,_]) -> cat;
ttt(X) -> no_winner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment