-module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1)....

9
-module(ficha1). - export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1,media/1]). -import(math,[pi/0]). %1. f2c(F) -> (F-32)/1.8. c2f(C) -> C*1.8+32. %2. convert({c,X}) -> X*1.8+23; convert({f,X}) -> (X-32)/1.8. %3. perimeter({square,Side}) -> Side*4; perimeter({circle,Radius}) -> pi*(Radius*Radius); perimeter({triangle,A,B,C}) -> A+B+C. %4. len([]) -> 0; len([H|T]) -> 1 + len(T). %5. exists(X,[]) -> false; exists(X,[H|T]) when X == H -> true; exists(X,[H|T]) -> exists(X,T).

Transcript of -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1)....

Page 1: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1

-module(ficha1).

-

export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca

picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1,media/1]).

-import(math,[pi/0]).

%1.

f2c(F) -> (F-32)/1.8.

c2f(C) -> C*1.8+32.

%2.

convert({c,X}) -> X*1.8+23;

convert({f,X}) -> (X-32)/1.8.

%3.

perimeter({square,Side}) -> Side*4;

perimeter({circle,Radius}) -> pi*(Radius*Radius);

perimeter({triangle,A,B,C}) -> A+B+C.

%4.

len([]) -> 0;

len([H|T]) -> 1 + len(T).

%5.

exists(X,[]) -> false;

exists(X,[H|T]) when X == H -> true;

exists(X,[H|T]) -> exists(X,T).

Page 2: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1

%6.

invert(L) -> invert(L,[]).

invert([H|T],L) -> invert(T,[H|L]);

invert([],L) -> L.

%7.

min([H|[]]) -> H;

min([H|T]) when H < hd(T) -> min([H|tl(T)]);

min([H|T]) -> min([hd(T)|tl(T)]).

%8.

max([H|[]]) -> H;

max([H|T]) when H > hd(T) -> max([H|tl(T)]);

max([H|T]) -> max([hd(T)|tl(T)]).

%9.

min_max(L) -> {min(L),max(L)}.

%10.

capicua([]) -> [];

capicua(L) -> capicua(L,invert(L)).

capicua(L1,L2) when L1 == L2 -> true;

capicua(L1,L2) -> false.

%11.

merge([]) -> [];

Page 3: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1

merge(L) -> merge(L,[]).

merge([H|T],L) when H /= [] -> merge([tl(H)|T],[hd(H)|L]);

merge([[]|T],L) -> merge(T,L);

merge([],L) -> invert(L).

%12.

substitute(A,B,[]) -> [];

substitute(A,B,[H|T]) when A == H -> substitute(A,B,[B|T]);

substitute(A,B,[H|T]) -> [H|substitute(A,B,T)].

%13.

count(A,[])->[];

count(A,L) -> count(A,L,0).

count(A,[H|T],Res) when A==H -> count(A,T,Res+1);

count(A,[H|T],Res) -> count(A,T,Res);

count(A,L,Res) -> Res.

%14.3

qsort([]) -> [];

qsort([H|T]) -> qsort([X || X <- T, X < H]) ++ [H] ++ qsort([Y || Y <- T, Y >= H]).

%15.1

derivada([H|[]]) -> [];

derivada([H|T]) when H < hd(T) -> [1|derivada(T)];

derivada([H|T]) when H > hd(T) -> [-1|derivada(T)].

%15.2

Page 4: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1

soma([]) -> 0;

soma([H|T]) -> H + soma(T).

media(L) -> soma(L) / len(L).

Page 5: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1
Page 6: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1
Page 7: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1
Page 8: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1
Page 9: -module(ficha1). %1. %2. - Apontamentos TSI · -module(ficha1). -export([f2c/1,c2f/1,convert/1,perimeter/1,len/1,exists/2,invert/1,min/1,max/1,min_max/1,ca picua/1,merge/1,substitute/3,count/2,qsort/1,derivada/1