1
-module(efficiency_guide).
5
naive_reverse([H|T]) ->
11
naive_but_ok_reverse([H|T], Acc) ->
12
naive_but_ok_reverse(T, [H]++Acc);
13
naive_but_ok_reverse([], Acc) ->
17
vanilla_reverse([H|T], Acc) ->
18
vanilla_reverse(T, [H|Acc]);
19
vanilla_reverse([], Acc) ->
23
multiple_setelement(T0) ->
24
T1 = setelement(9, T0, bar),
25
T2 = setelement(7, T1, foobar),
26
setelement(5, T2, new_value).
29
my_list_to_binary(List) ->
30
my_list_to_binary(List, <<>>).
32
my_list_to_binary([H|T], Acc) ->
33
my_list_to_binary(T, <<Acc/binary,H>>);
34
my_list_to_binary([], Acc) ->
37
my_old_list_to_binary(List) ->
38
my_old_list_to_binary(List, []).
40
my_old_list_to_binary([H|T], Acc) ->
41
my_old_list_to_binary(T, [Acc,H]);
42
my_old_list_to_binary([], Acc) ->
45
my_binary_to_list(<<H,T/binary>>) ->
46
[H|my_binary_to_list(T)];
47
my_binary_to_list(<<>>) -> [].
49
my_complicated_binary_to_list(Bin) ->
50
my_complicated_binary_to_list(Bin, 0).
52
my_complicated_binary_to_list(Bin, Skip) ->
54
<<_:Skip/binary,Byte,_/binary>> ->
55
[Byte|my_complicated_binary_to_list(Bin, Skip+1)];
60
after_zero(<<0,T/binary>>) ->
62
after_zero(<<_,T/binary>>) ->
67
all_but_zeroes_to_list(Buffer, Acc, 0) ->
68
{lists:reverse(Acc),Buffer};
69
all_but_zeroes_to_list(<<0,T/binary>>, Acc, Remaining) ->
70
all_but_zeroes_to_list(T, Acc, Remaining-1);
71
all_but_zeroes_to_list(<<Byte,T/binary>>, Acc, Remaining) ->
72
all_but_zeroes_to_list(T, [Byte|Acc], Remaining-1).
74
non_opt_eq([H|T1], <<H,T2/binary>>) ->
76
non_opt_eq([_|_], <<_,_/binary>>) ->
78
non_opt_eq([], <<>>) ->
81
opt_eq(<<H,T1/binary>>, [H|T2]) ->
83
opt_eq(<<_,_/binary>>, [_|_]) ->
88
match_head(List, <<_:10,Data/binary>>) ->
89
match_body(List, Data).
91
match_body([0|_], <<H,_/binary>>) ->
93
match_body([H|T1], <<H,T2/binary>>) ->
96
count1(<<_,T/binary>>, Count) -> count1(T, Count+1);
97
count1(<<>>, Count) -> Count.
99
count2(<<H,T/binary>>, Count) -> count2(T, Count+1);
100
count2(<<>>, Count) -> Count.
102
count3(<<_H,T/binary>>, Count) -> count3(T, Count+1);
103
count3(<<>>, Count) -> Count.
108
fib(0, _Current, _Next, Fibs) ->
110
fib(N, Current, Next, Fibs) ->
111
fib(N - 1, Next, Current + Next, [Current|Fibs]).
114
recursive_fib(N, 0, 1).
116
recursive_fib(0, _Current, _Next) ->
118
recursive_fib(N, Current, Next) ->
119
[Current|recursive_fib(N - 1, Next, Current + Next)].
122
bad_fib(N, 0, 1, []).
124
bad_fib(0, _Current, _Next, Fibs) ->
126
bad_fib(N, Current, Next, Fibs) ->
127
bad_fib(N - 1, Next, Current + Next, Fibs ++ [Current]).
129
tail_recursive_fib(N) ->
130
tail_recursive_fib(N, 0, 1, []).
132
tail_recursive_fib(0, _Current, _Next, Fibs) ->
134
tail_recursive_fib(N, Current, Next, Fibs) ->
135
tail_recursive_fib(N - 1, Next, Current + Next, [Current|Fibs]).
137
append([H|T], Tail) ->
148
kilo_byte(N-1, [Acc|Acc]).
150
recursive_sum([H|T]) ->
152
recursive_sum([]) -> 0.
156
sum([H|T], Sum) -> sum(T, Sum + H);
160
element(M, {31,28,31,30,31,30,31,31,30,31,30,31}).
164
atom_map1(three) -> 3;
165
atom_map1(Int) when is_integer(Int) -> Int;
166
atom_map1(four) -> 4;
167
atom_map1(five) -> 5;
172
atom_map2(three) -> 3;
173
atom_map2(four) -> 4;
174
atom_map2(five) -> 5;
176
atom_map2(Int) when is_integer(Int) -> Int.
178
atom_map3(Int) when is_integer(Int) -> Int;
181
atom_map3(three) -> 3;
182
atom_map3(four) -> 4;
183
atom_map3(five) -> 5;
187
map_pairs1(_Map, [], Ys) ->
189
map_pairs1(_Map, Xs, [] ) ->
191
map_pairs1(Map, [X|Xs], [Y|Ys]) ->
192
[Map(X, Y)|map_pairs1(Map, Xs, Ys)].
194
map_pairs2(_Map, [], Ys) ->
196
map_pairs2(_Map, [_|_]=Xs, [] ) ->
198
map_pairs2(Map, [X|Xs], [Y|Ys]) ->
199
[Map(X, Y)|map_pairs2(Map, Xs, Ys)].
201
explicit_map_pairs(Map, Xs0, Ys0) ->
206
[Map(X, Y)|explicit_map_pairs(Map, Xs, Ys)];