Skip to content

Instantly share code, notes, and snippets.

View apbarrero's full-sized avatar

Antonio Pérez Barrero apbarrero

View GitHub Profile
@apbarrero
apbarrero / first.erl
Created June 21, 2017 20:17
First erlang program
-module(first).
-export([double/1,mult/2,area/3,sq/1,treble/1]).
mult(X,Y) ->
X*Y.
double(X) ->
mult(2,X).
area(A,B,C) ->
-module(recurs).
-export([fib/1, pieces/1]).
fib(1) ->
[0];
fib(2) ->
[1,0];
fib(N) when N>2 ->
L = fib(N-1),
[X|Xs] = L,
@apbarrero
apbarrero / tailrecurs.erl
Last active June 24, 2017 13:52
Tail recursion exercise
-module(tailrecurs).
-export([fib/1, perfect/1, testPerfect/0]).
fib(0) -> 0;
fib(1) -> 1;
fib(N) when N>1 ->
fib(N, 1, 0).
fib(2, Acc1, Acc2) -> Acc1 + Acc2;
fib(N, Acc1, Acc2) when N>2 -> fib(N-1, Acc1 + Acc2, Acc1).
@apbarrero
apbarrero / lists1.erl
Created June 27, 2017 20:53
Defining functions over lists in practice
-module(lists1).
-export([product/1, maximum/1]).
product(Xs) -> product(Xs, 1).
product([], P) -> P;
product([X|Xs], P) -> product(Xs, X*P).
maximum([X|Xs]) -> maximum(Xs, X).
maximum([X|[]], M) -> max(X, M);
maximum([X|Xs], M) -> maximum(Xs, max(X, M)).