-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.erl
42 lines (32 loc) · 1011 Bytes
/
general.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
%%%-------------------------------------------------------------------
%%% @author edith
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 15. Mar 2020 22:06
%%%-------------------------------------------------------------------
-module(general).
-author("edith").
%% API
-export([reverse/1, find/2, delete/2, flatten/1, square/1, square2/1, square3/1, filter1/2]).
reverse([]) -> [];
reverse([X|Xs]) -> reverse(Xs) ++ [X].
find(_, []) -> not_found;
find(X, [X|_]) -> {found, X};
find(X, [_|Tail]) -> find(X, Tail).
delete(_,[]) -> [];
delete(X, [X|Tail]) -> Tail;
delete(X, [H|Tail]) ->[H] ++ delete(X,Tail).
flatten([]) -> [];
flatten([Xs|Ys]) -> Xs ++ flatten(Ys).
square([]) -> [];
square([X|Ys]) -> [X*X] ++ square(Ys).
square2(Alist) -> [X*X || X <- Alist].
square3(Xs) -> lists:map(fun(X) -> X*X end, Xs).
filter1(_, []) -> [];
filter1(F, [X|Xs]) ->
case F(X) of
true -> [X|filter1(F, Xs)] ;
false -> filter1(F, Xs)
end.