Last active
March 8, 2017 21:55
-
-
Save jason-carter/77090542affa78a02472ba7fabc4c5cb to your computer and use it in GitHub Desktop.
FutureLearn Functional Programming In Erlang 2.18: Consolidation: functions over lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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]. | |
join([],[]) -> []; | |
join(X, []) -> X; | |
join([], Y) -> Y; | |
% The following two functions replaced by the one below following | |
% comment by Leandro Lopez-Niederer. Much simpler! | |
%join(X, Y) -> join(X, Y, []). | |
%join(X, Y, ReverseX) -> join([], shunt(shunt(X, ReverseX), Y)). | |
join(X, Y) -> shunt(shunt(X, []), Y). | |
shunt([],Ys) -> Ys; | |
shunt([X|Xs],Ys) -> shunt(Xs,[X|Ys]). | |
join_test() -> | |
"hello" = join("hel", "lo"), | |
[5,4,3,2,1] = join([5,4], [3,2,1]), | |
test_join_passed. | |
% My implementation of lists:concat | |
-spec concat([T]) -> [T]. | |
% Not sure if [T] is correct since it's supposed to be a list of lists | |
concat(List) -> concat(List, []). | |
concat([], Result) -> Result; | |
concat([ListH | ListT], Result) -> concat(ListT, join(Result, ListH)). | |
concat_test() -> | |
"goodbye" = concat(["goo","d","","by","e"]), | |
[1,2,3,4,5,6] = concat([[1,2], [3,4,5], [6]]), | |
concat_test_passed. | |
% Testing membership | |
-spec member(T, [T]) -> boolean(). | |
member(_, []) -> false; | |
member(Elem, [Elem | _ListT]) -> true; | |
member(Elem, [_ListH | ListT]) -> member(Elem, ListT). | |
% Sorting lists | |
% | |
%TODO: Will return to this when I have more time | |
% Permutations | |
% | |
%TODO: Will return to this when I have more time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment