Last active
August 29, 2015 14:04
-
-
Save nkoneko/5f95e3bf57be6a2077d4 to your computer and use it in GitHub Desktop.
Exercises in http://www.erlang.org/course/exercises.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(concurrent1). | |
-export([pong/0, ping/1, start/1]). | |
pong() -> | |
receive | |
{From, Msg} -> | |
io:format("Pong received ~w~n", [Msg]), | |
From ! {self(), Msg}, | |
pong(); | |
term -> | |
io:format("Bye~n", []) | |
end. | |
ping(0) -> | |
pongProc ! term, | |
io:format("Bye~n", []); | |
ping(M) -> | |
pongProc ! {self(), hello}, | |
receive | |
{From, Msg} -> | |
io:format("Ping received ~w~n", [Msg]) | |
end, | |
ping(M - 1). | |
start(M) -> | |
PongPid = spawn(concurrent1, pong, []), | |
register(pongProc, PongPid), | |
spawn(concurrent1, ping, [M]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(list1). | |
-export([min/1, max/1, min_max/1]). | |
min([Head | Rest]) -> | |
min_(Rest, Head). | |
max([Head | Rest]) -> | |
max_(Rest, Head). | |
min_([], Result) -> | |
Result; | |
min_([Head | Rest], ResultSoFar) when ResultSoFar > Head -> | |
min_(Rest, Head); | |
min_([_ | Rest], ResultSoFar) -> | |
min_(Rest, ResultSoFar). | |
max_([], Result) -> | |
Result; | |
max_([Head | Rest], ResultSoFar) when ResultSoFar < Head -> | |
max_(Rest, Head); | |
max_([_ | Rest], ResultSoFar) -> | |
max_(Rest, ResultSoFar). | |
min_max(List) -> | |
{list1:min(List), list1:max(List)}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(temp). | |
-export([convert/1]). | |
f2c(F) -> | |
5 * (F - 32) / 9. | |
c2f(C) -> | |
9 * C / 5 + 32. | |
convert({c, X}) -> | |
{f, c2f(X)}; | |
convert({f, Y}) -> | |
{c, f2c(Y)}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment