Skip to content

Instantly share code, notes, and snippets.

@rdiaz82
Last active October 11, 2017 07:40
Show Gist options
  • Save rdiaz82/da9f123b619a552a37e5d17aca39d641 to your computer and use it in GitHub Desktop.
Save rdiaz82/da9f123b619a552a37e5d17aca39d641 to your computer and use it in GitHub Desktop.
Erlang Exercises

This gist will cover my own exercise implementation for the erlang exercises extracted from the Erlang Site Course: http://erlang.org/course/exercises.html, some exercises from the "Programming Erlang, Software for a Concurrent World" by Joe Armstrong and some crazy ideas to train myself :P

-module(temperature).
-export ([tempConvert/1]).
tempConvert({c, Temp}) -> {f, (9*Temp/5)+32};
tempConvert({f, Temp}) -> {c, 5*(Temp-32)/9}.
---- Shell Output ----
1> temperature:tempConvert({f, 68}).
{c,20.0}
2> temperature:tempConvert({f, 32}).
{c,0.0}
3> temperature:tempConvert({c, 20}).
{f,68.0}
-module(demo).
-export([double/1]).
double(X) -> 2*X.
---- Shell Output ----
1> c(demo).
{ok,demo}
2> demo:double(6).
12
Write a function which starts N processes in a ring, and sends a message M times around all the processes in the ring.
After the messages have been sent the processes should terminate gracefully.
-module(loop).
-export([start/3]).
start(NumberNodes, Message, Times) ->
FirstNode = spawn(fun()->loop(1) end),
io:format("Created node ~p: ~p~n", [1, FirstNode]),
createNode(NumberNodes-1,2,FirstNode),
FirstNode ! {Message, 1, Times}.
createNode(NumberNodes,CurrentNode,DestinationNode) when CurrentNode=< NumberNodes ->
Node= spawn(fun()->loop(DestinationNode)end),
io:format("Created node ~p: ~p~n", [CurrentNode, Node]),
createNode(NumberNodes,CurrentNode+1, Node);
createNode(_,CurrentNode,DestinationNode) ->
register(last_node, spawn(fun()->loop(DestinationNode)end)),
io:format("Created node ~p: ~p~n",[CurrentNode, whereis(last_node)]).
loop(1)->
receive
{Message, CurrentIteration, MaxIteration} when CurrentIteration =< MaxIteration->
io:format("Received at ~p the message: ~p~n",[self(),Message]),
whereis(last_node) ! {Message, CurrentIteration+1,MaxIteration},
loop(1);
{_, _, _} ->
whereis(last_node) ! {kill},
io:format("Exit process ~p~n", [self()]),
erlang:exit(self(),ok)
end;
loop(Destination) ->
receive
{Message, CurrentIteration, MaxIteration} ->
io:format("Received at ~p the message: ~p~n",[self(),Message]),
Destination ! {Message, CurrentIteration,MaxIteration},
loop(Destination);
{kill} ->
Destination ! {kill},
io:format("Exit process ~p~n", [self()]),
erlang:exit(self(),ok)
end.
---- Shell Output ----
1> loop:start(6).
Created node 1: <0.681.0>
Created node 2: <0.682.0>
Created node 3: <0.683.0>
Created node 4: <0.684.0>
Created node 5: <0.685.0>
Created node 6: <0.686.0>
ok
2> loop:sendMessage("Hola",3).
Received at <0.681.0> the message: "Hola"
Received at <0.686.0> the message: "Hola"
Received at <0.685.0> the message: "Hola"
Received at <0.684.0> the message: "Hola"
Received at <0.683.0> the message: "Hola"
Received at <0.682.0> the message: "Hola"
Received at <0.681.0> the message: "Hola"
Received at <0.686.0> the message: "Hola"
Received at <0.685.0> the message: "Hola"
Received at <0.684.0> the message: "Hola"
Received at <0.683.0> the message: "Hola"
Received at <0.682.0> the message: "Hola"
Received at <0.681.0> the message: "Hola"
Received at <0.686.0> the message: "Hola"
Received at <0.685.0> the message: "Hola"
Received at <0.684.0> the message: "Hola"
Received at <0.683.0> the message: "Hola"
Received at <0.682.0> the message: "Hola"
Exit process <0.681.0>
Exit process <0.686.0>
Exit process <0.685.0>
Exit process <0.684.0>
Exit process <0.683.0>
Exit process <0.682.0>
-module(list).
-export([listMin/1, listMax/1, listMinMax/1, listMinMax2/1]).
listMin([H|T]) -> listMin(H,T);
listMin([]) -> erlang:error(empty_list).
listMin(X,[H|T]) when X=<H -> listMin(X,T);
listMin(X,[H|T]) when X>H -> listMin(H,T);
listMin(X,[]) -> X.
listMax([H|T]) -> listMax(H,T);
listMax([]) -> erlang:error(empty_list).
listMax(X,[H|T]) when X=<H -> listMax(H,T);
listMax(X,[H|T]) when X>H -> listMax(X,T);
listMax(X,[]) -> X.
listMinMax(X)->{listMin(X), listMax(X)}.
listMinMax2([H|T])->listMinMax2(H,H,T).
listMinMax2(Min,Max,[H|T]) when Min=<H, Max=<H -> listMinMax2(Min,H,T);
listMinMax2(Min,Max,[H|T]) when Min=<H, Max>H -> listMinMax2(Min,Max,T);
listMinMax2(Min,Max,[H|T]) when Min>H, Max=<H -> listMinMax2(H,H,T);
listMinMax2(Min,Max,[H|T]) when Min>H, Max>H -> listMinMax2(H,Max,T);
listMinMax2(Min,Max,[])->{Min,Max}.
---- Shell Output ----
1> list:listMin([1,2,3,4,0,6,78,56,23,12]).
0
2> list:listMax([1,2,3,4,0,6,78,56,23,12]).
78
3> list:listMinMax([1,2,3,4,0,6,78,56,23,12]).
{0,78}
4> list:listMinMax2([1,2,3,4,0,6,78,56,23,12]).
{0,78}
5> timer:tc(list, listMin, [[1,2,3,4,0,6,78,56,23,12]]).
{5,0}
6> timer:tc(list, listMax, [[1,2,3,4,0,6,78,56,23,12]]).
{5,78}
7> timer:tc(list, listMinMax, [[1,2,3,4,0,6,78,56,23,12]]).
{7,{0,78}}
8> timer:tc(list, listMinMax2, [[1,2,3,4,0,6,78,56,23,12]]).
{5,{0,78}}
-module(mathStuff).
-export ([perimeter/1]).
perimeter({square, Side}) -> 4*Side;
perimeter({triangle, A, B, C}) -> A + B + C;
perimeter({circle, Radius}) -> 2 * 3.1416 * Radius;
perimeter(_) -> erlang:error(invalid_param).
---- Shell Output ----
1> mathStuff:perimeter({squre,2}).
4
2> mathStuff:perimeter({circle,2}).
12.5664
3> mathStuff:perimeter({triangle,2,3,4}).
9
4> mathStuff:perimeter({hexagon,5,6,7,8,9,5}).
** exception error: invalid_param
in function mathStuff:perimeter/1 (mathStuff.erl, line 7)
-module(process).
-export([start/0, sendMessage/4]).
start() -> {spawn(fun() -> loop() end), spawn(fun() -> loop() end)}.
loop()->
receive
{From, Current, Max, Message} when Current=<Max ->
io:format("~p - ~p received ~p ~n", [Current, self(), Message]),
From ! {self(), Current+1, Max, Message},
loop();
{From, _, _, _} ->
io:format("Exit: ~p ~n", [From]),
exit(From, ok),
io:format("Exit: ~p ~n", [self()]),
exit(self(), ok)
end.
sendMessage(Process1, Process2, Message, Times) ->
Process2 ! {Process1, 0, Times, Message},
io:format("Init Loop~n").
---- Shell Output ----
1> {Process1, Process2} = process:start().
{<0.147.0>,<0.148.0>}
2> process:sendMessage(Process1, Process2, "hola", 20).
Init Loop
0 - <0.148.0> received "hola"
1 - <0.147.0> received "hola"
2 - <0.148.0> received "hola"
3 - <0.147.0> received "hola"
4 - <0.148.0> received "hola"
5 - <0.147.0> received "hola"
6 - <0.148.0> received "hola"
7 - <0.147.0> received "hola"
8 - <0.148.0> received "hola"
9 - <0.147.0> received "hola"
10 - <0.148.0> received "hola"
11 - <0.147.0> received "hola"
12 - <0.148.0> received "hola"
13 - <0.147.0> received "hola"
14 - <0.148.0> received "hola"
15 - <0.147.0> received "hola"
16 - <0.148.0> received "hola"
17 - <0.147.0> received "hola"
18 - <0.148.0> received "hola"
19 - <0.147.0> received "hola"
20 - <0.148.0> received "hola"
Exit: <0.148.0>
Exit: <0.147.0>
-module(perimeter_server).
-export([start/0, listener/2]).
start() -> spawn(fun() -> loop() end).
loop() ->
receive
{From, {square, Side}} ->
From ! {self(), 4*Side},
loop();
{From, {triangle,A,B,C}} ->
From ! {self(), A+B+C},
loop();
{From, {circle, Radius}} ->
From ! {self(), 2*3.1416*Radius},
loop();
{From, _} ->
From ! {error, "invalid shape"},
loop()
end.
listener(Pid, Request) -> Pid ! {self(), Request},
receive
{Pid, Response} -> Response
end.
---- Shell Output ----
1> Server = perimeter_server:start().
<0.201.0>
2> perimeter_server:listener(Server, {square,3}).
12
3> perimeter_server:listener(Server, {circle,3}).
18.8496
4> perimeter_server:listener(Server, {triangle,3,2,3}).
8
---- Pascal triangle implementation: ----
-module(triangle).
-export ([triangle/1]).
create_row([]) -> [1];
create_row([H|T])->[H|create_row(H,T)].
create_row(X,[H|T])->[X+H | create_row(H,T)];
create_row(X,[])->[X].
triangle(Max)->triangle_in(1, Max, {[1]}).
triangle_in(Current, Max, Tuple) when Current<Max -> triangle_in(Current+1,Max,erlang:insert_element(1,Tuple,create_row(erlang:element(1,Tuple))));
triangle_in(Current,Max,Tuple) when Current==Max -> Tuple.
---- Shell Output ----
1> triangle:triangle(5).
{[1,4,6,4,1],[1,3,3,1],[1,2,1],[1,1],[1]}
2> triangle:triangle(10).
{[1,9,36,84,126,126,84,36,9,1],
[1,8,28,56,70,56,28,8,1],
[1,7,21,35,35,21,7,1],
[1,6,15,20,15,6,1],
[1,5,10,10,5,1],
[1,4,6,4,1],
[1,3,3,1],
[1,2,1],
[1,1],
[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment