-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial.erl
309 lines (246 loc) · 6.97 KB
/
tutorial.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
%% @author Karl Marklund <[email protected]>
-module(tutorial).
-export([hello/0, hello/1,
fac/1, fib/1,
fac_tr/1, fib_tr/1,
right_triangles/1,
simpsons/0, simpsons/1,
char_to_upper/1, char_to_lower/1,
str_to_upper/1, str_to_lower/1,
max/1, count/2,
odd_and_even/1
]).
%% @doc Prints "Hello!" to the terminal.
-spec hello() -> ok.
hello() ->
io:format("Hello!~n").
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Recursive functions %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Prints "Hello!" recursively.
-spec hello(N::integer()) -> ok.
hello(0) ->
ok;
hello(N) ->
io:format("~p Hello!~n", [N]),
hello(N-1).
%% @doc The factorial function.
%% === Example ===
%% <div class="example">```
%% 25> [{N,tutorial:fac(N)} || N <- lists:seq(0,10)].
%% [{0,1},
%% {1,1},
%% {2,2},
%% {3,6},
%% {4,24},
%% {5,120},
%% {6,720},
%% {7,5040},
%% {8,40320},
%% {9,362880},
%% {10,3628800}]'''
%% </div>
-spec fac(N::integer()) -> integer().
fac(0) -> 1;
fac(N) -> N*fac(N-1).
%% @doc Calcultates the Nth Fibonnacci number.
%% === Example ===
%% <div class="example">```
%% > [tutorial:fib(N) || N <- lists:seq(0,10)].
%% [0,1,1,2,3,5,8,13,21,34,55]'''
%% </div>
-spec fib(N::integer()) -> integer().
fib(0) ->
0;
fib(1) ->
1;
fib(N) when N > 0 ->
fib(N - 1) + fib(N - 2).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Tail Recursive functions %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc The factorial function, implemented using tail recursion.
-spec fac_tr(N::integer()) -> integer().
fac_tr(N) ->
fac_tr(N,1).
fac_tr(0, Acc) ->
Acc;
fac_tr(N, Acc) ->
fac_tr(N-1, Acc*N).
%% @doc Calculates the Nth fibonacci number, implemented using tail
%% recursion.
-spec fib_tr(N::integer()) -> integer().
fib_tr(N) ->
fib_tr(N, 0, 1).
fib_tr(0, Xi, Xii) ->
Xi;
fib_tr(Iter, Xi, Xii) ->
fib_tr(Iter, Xi + Xii, Xi).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% List Comprehensions %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Generates a list of tuples {A,B,C} such that A and B are sides
%% in a right triangle with hypotenuse C, where `A,B,C <= N'.
%% === Example ===
%% <div class="example">```
%% > tutorial:right_triangles(10).
%% [{3,4,5},{4,3,5},{6,8,10},{8,6,10}]'''
%% </div>
-spec right_triangles(N) -> [{A,B,C}] when
N::integer(),
A::integer(),
B::integer(),
C::integer().
right_triangles(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
C =< N,
A*A+B*B == C*C
].
%% @doc Returns a list of tuples, where each tuple describes a caracter in the Simposon family.
%%
%% === Example ===
%% <div class="example">```
%% > tutorial:simpsons().
%% [{person,male,"Bart"},
%% {cat,female,"Snowball II"},
%% {person,male,"Homer"},
%% {person,female,"Lisa"},
%% {dog,male,"Santa's Little Helper"},
%% {person,female,"Marge"},
%% {pig,male,"Spider Pig"}]'''
%% </div>
-spec simpsons() -> [{Type, Gender, Name}] when
Type::person|cat|dog|pig,
Gender::male|female,
Name::string().
simpsons() ->
[
{person, male, "Bart"},
{cat, female, "Snowball II"},
{person, male, "Homer"},
{person, female, "Lisa"},
{dog, male, "Santa's Little Helper"},
{person, female, "Marge"},
{pig, male, "Spider Pig"}
].
%% @doc Returns a filtered list of names of characters in the Simpson family.
%% === Example ===
%% <div class="example">```
%% > tutorial:simpsons(names).
%% ["Bart","Snowball II","Homer","Lisa",
%% "Santa's Little Helper","Marge","Spider Pig"]
%% > tutorial:simpsons(females).
%% ["Snowball II","Lisa","Marge"]
%% > tutorial:simpsons(males).
%% ["Bart","Homer","Santa's Little Helper","Spider Pig"]
%% > tutorial:simpsons(pets).
%% ["Snowball II","Santa's Little Helper","Spider Pig"]'''
%% </div>
-spec simpsons(Filter) -> [Name] when
Filter::names|males|females|pets,
Name::string().
simpsons(names) ->
[X || {_, _, X} <- simpsons()];
simpsons(males) ->
[X || {_, male, X} <- simpsons()];
simpsons(females) ->
[X || {_, female, X} <- simpsons()];
simpsons(pets) ->
[X || {Y, _, X} <- simpsons(), Y /= person].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Guarded Functions %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Convert a character to upper case.
%% === Example ===
%% <div class="example">```
%% > tutorial:char_to_upper($a).
%% 65
%% > tutorial:char_to_upper($@).
%% 64'''
%% </div>
-spec char_to_upper(char()) -> char().
char_to_upper(Char) when (Char > 96) and (Char < 123) ->
Char - 32;
char_to_upper(Char) ->
Char.
%% @doc Convert a character to lower case.
%% === Example ===
%% <div class="example">```
%% > tutorial:char_to_lower($A).
%% 97
%% > tutorial:char_to_lower($@).
%% 64'''
%% </div>
-spec char_to_lower(char()) -> char().
char_to_lower(Char) when true ->
tbi.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Map %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HINT: Use the char_to_upper() and char_to_lower().
%% @doc Convert a string to upper case.
%% === Example ===
%% <div class="example">```
%% > tutorial:str_to_upper("Erlang").
%% "ERLANG"'''
%% </div>
-spec str_to_upper(string()) -> string().
str_to_upper(String) ->
tbi.
%% @doc Convert a string to lower case.
%% === Example ===
%% <div class="example">```
%% 7> tutorial:str_to_lower("Upper + Lower").
%% "upper + lower"'''
%% </div>
-spec str_to_lower(string()) -> string().
str_to_lower(String) ->
tbi.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Fold %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Returns the max value M in a list L.
%% === Example ===
%% <div class="example">```
%% 8> tutorial:max([4,-1,8, 0, 3]).
%% 8'''
%% </div>
-spec max(L) -> M when
L::[integer()],
M::integer().
max([H | T]) ->
F = tbi,
lists:foldl(F, H, T).
%% @doc Returns the number of times Char occurs in String.
%% === Example ===
%% <div class="example">```
%% > tutorial:count("Operating systems and multicore programming", $m).
%% 4'''
%% </div>
-spec count(String, Char) -> integer() when
String::string(),
Char::char().
count(String, Char) ->
F = tbi,
lists:foldl(F, 0, String).
%% @doc Returns a tuple {{odd, Odd}, {even, Even}} where Odd and Even
%% are lists with all the odd and even numbers in List.
%% === Example ===
%% <div class="example">```
%% > tutorial:odd_and_even(lists:seq(1,10)).
%% {{odd,[9,7,5,3,1]},{even,[10,8,6,4,2]}}'''
%% </div>
-spec odd_and_even(List) -> {{odd, Odd},{even, Even}} when
List::[integer()],
Odd::[integer()],
Even::[integer()].
odd_and_even(List) ->
F = fun(X, {{odd, Odd}, {even, Even}}) when X rem 2 == 0->
{{odd, Odd}, {even, [X | Even]}};
(X, {{odd, Odd}, {even, Even}}) ->
{{odd, [X | Odd]}, {even, Even}}
end,
lists:foldl(F, {{odd, []}, {even, []}}, List).