Skip to content

Instantly share code, notes, and snippets.

@jason-carter
jason-carter / rps2.erl
Created March 23, 2017 16:12
FutureLearn Functional Programming In Erlang 3.12: Strategies exercises
-module(rps2).
-export([play/1,echo/1,play_two/3,rock/1,no_repeat/1,const/1,enum/1,cycle/1,rand/1,val/1,tournament/2]).
%
% play one strategy against another, for N moves.
%
play_two(StrategyL,StrategyR,N) ->
play_two(StrategyL,StrategyR,[],[],N).
@jason-carter
jason-carter / rps.erl
Created March 18, 2017 21:29
FutureLearn Functional Programming In Erlang 3.8: Modelling the basics of rock-paper-scissors
-module(rps).
-export([result/2]).
-export([tournament/2]).
% Win and Lose
beat(rock) -> paper;
beat(paper) -> scissors;
beat(scissors) -> rock.
lose(rock) -> scissors;
@jason-carter
jason-carter / hofs.erl
Created March 17, 2017 14:21
FutureLearn Functional Programming In Erlang 3.5: Higher-order functions in practice
-module(hofs).
-export([doubleAll/1]).
-export([evens/1]).
-export([product/1]).
-export([zip/2]).
-export([zip2/2]).
-export([zip_with/3]).
-export([zip_with2/3]).
% Using higher-order functions
@jason-carter
jason-carter / index.erl
Created March 15, 2017 15:12
FutureLearn Functional Programming In Erlang 2.20: Programming challenge: indexing a file
-module(index).
-export([get_file_contents/1,show_file_contents/1]).
-export([wordindex/1, test/1]).
%
% Indexing a file
% Given a text file, return a list of words and the ranges of lines on which it occurs
%
-spec wordindex(string()) -> [{string(), [{integer(),integer()}]}].
@jason-carter
jason-carter / consolidation.erl
Last active March 8, 2017 21:55
FutureLearn Functional Programming In Erlang 2.18: Consolidation: functions over lists
-module(consolidation).
-export([join/2, join_test/0]).
-export([concat/1, concat_test/0]).
-export([member/2]).
% Joining Lists
%
% My implementation of ++ called join/2 (which uses shunt from previous modules)
-spec join([T], [T]) -> [T].
@jason-carter
jason-carter / palindrome.erl
Created March 7, 2017 20:02
FutureLearn Functional Programming In Erlang 2.15: The 'palindrome' function
-module(palindrome).
-export([palindrome/1, clean/1, clean/2, palindrome_test/0]).
% palindrome("Madam I\'m Adam") = true
palindrome([]) -> [];
palindrome(List) ->
CleanList = clean(List),
ReversedList = lists:reverse(CleanList),
@jason-carter
jason-carter / dedupe2.erl
Created March 7, 2017 11:03
FutureLearn Functional Programming In Erlang 2.13: The 'nub' function
-module(dedupe2).
-export([exists/2,nubfb/1,nubfb/2,nubbf/1,nubbf/2,nub_test/0]).
% nubfb([2,4,1,3,3,1]) = [2,4,1,3]
% nubbf([2,4,1,3,3,1]) = [2,4,3,1]
exists(Val, []) -> [Val];
exists(Val, [X|_Xs]) when Val == X -> [];
exists(Val, [X|Xs]) when Val =/= X -> exists(Val, Xs).
@jason-carter
jason-carter / dedupe.erl
Created March 7, 2017 11:02
FutureLearn Functional Programming In Erlang 2.13: The 'nub' function
-module(dedupe).
-export([exists/2,nubfb/1,nubfb/2,nubbf/1,nubbf/2,nub_test/0]).
% nubfb([2,4,1,3,3,1]) = [2,4,1,3]
% nubbf([2,4,1,3,3,1]) = [2,4,3,1]
exists(_, []) -> false;
exists(Val, [X|_Xs]) when Val == X -> true;
exists(Val, [X|Xs]) when Val =/= X -> exists(Val, Xs).
@jason-carter
jason-carter / take.erl
Last active March 6, 2017 19:46
FutureLearn Functional Programming In Erlang 2.11: Where do I begin
-module(take).
-export([take/2, take_test/0]).
-spec take(integer(), [T]) -> [T].
take(0, _) -> [];
take(_, []) -> [];
take(Cnt, [X|Xs]) when Cnt > 0 -> [X | take(Cnt-1, Xs)].
take_test() ->
[] = take(0,"hello"),
@jason-carter
jason-carter / listcons.erl
Created March 3, 2017 16:12
FutureLearn Functional Programming In Erlang 2.9: Constructing lists with recursive functions
-module(listcons).
-export([double/1]).
-export([doubleelems/1]).
-export([even/1,evens/1]).
% Erlang Course activity 2.9
% Transforming List Elements
% Define an Erlang function double/1 to double the elements of a list of numbers.