-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolidation.erl
54 lines (39 loc) · 1.21 KB
/
consolidation.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-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;
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