-
Notifications
You must be signed in to change notification settings - Fork 4
/
forloop.erl
44 lines (36 loc) · 1.48 KB
/
forloop.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
-module(forloop).
-export([for/2, for/3]).
% Esempio di esecuzione:
%
% > c(forloop).
% {ok,forloop}
% > forloop:for(fun(X) -> X end, 10).
% [0,1,2,3,4,5,6,7,8,9,10]
% > forloop:for(fun(X) -> X end, -10).
% [0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
% > forloop:for(fun(X) -> X end, 5, 10).
% [5,6,7,8,9,10]
% > forloop:for(fun(X) -> X end, 10, 5).
% [10,9,8,7,6,5]
% > forloop:for(fun(X) -> X end, 10, 10).
% "\n"
% > forloop:for(fun(X) -> X end, 1, 1).
% [1]
% > forloop:for(fun(X) -> X*X end, 0, 20).
% [0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400]
%% funzioni esportate %%
% con le guard verifico se l'incremento deve essere positivo o negativo
for(F, Max) when Max < 0 -> for(F, 0, Max, -1);
for(F, Max) -> for(F, 0, Max, +1).
for(F, Min, Max) when Min > Max -> for(F, Min, Max, -1);
for(F, Min, Max) -> for(F, Min, Max, +1).
%% funzioni ausiliarie %%
% non utilizzo la ricorsione in coda perché:
% (https://www.erlang.org/doc/efficiency_guide/myths.html)
% "A body-recursive function generally uses the same amount of memory as
% a tail-recursive function. It is generally not possible to predict
% whether the tail-recursive or the body-recursive version will be
% faster. Therefore, use the version that makes your code cleaner (hint:
% it is usually the body-recursive version)."
for(F, Max, Max, _) -> [F(Max)]; % fa il match quando il secondo e il terzo parametro sono uguali
for(F, I, Max, Inc) -> [F(I) | for(F, I+Inc, Max, Inc)].