Created
April 22, 2011 08:26
-
-
Save tarnacious/936268 to your computer and use it in GitHub Desktop.
Hello World Process Chain
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(accumulators). | |
-export([average/1]). | |
average(X) -> average(X, 0, 0). | |
average([H|T], Length, Sum) -> | |
average(T, Length + 1, Sum + H); | |
average([], Length, Sum) -> | |
Sum / Length. |
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
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:8:8] [rq:8] [async-threads:0] [hipe] [kernel-poll:false] | |
Eshell V5.8.3 (abort with ^G) | |
1> c(accumulators). | |
{ok,accumulators} | |
2> accumulators:average([1,10,100]). | |
37.0 | |
3> |
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(hello_world). | |
-compile(export_all). | |
main() -> | |
HelloPid = spawn(fun() -> hello_node(none) end), | |
WorldPid = spawn(fun() -> world_node(none) end), | |
PrintPid = spawn(fun() -> print_node() end), | |
io:format("Send link messages~n"), | |
HelloPid ! {next, WorldPid}, | |
WorldPid ! {next, PrintPid}, | |
io:format("Sent messages~n"), | |
HelloPid ! {message, "I say"}, | |
ok. | |
hello_node(NextPid) -> | |
receive | |
{next, Pid} -> | |
io:format("Hello Process (~p) linked to ~p~n", [self(), Pid]), | |
hello_node(Pid); | |
{message, Message} -> | |
io:format("Hello Process (~p) receices message (~p)~n", [self(), Message]), | |
NextPid ! {message, string:concat(Message," Hello")}, | |
hello_node(NextPid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]) | |
end, | |
ok. | |
world_node(NextPid) -> | |
receive | |
{next, Pid} -> | |
io:format("World Process (~p) linked to ~p~n", [self(), Pid]), | |
world_node(Pid); | |
{message, Message} -> | |
io:format("World Process (~p) receives message (~p)~n", [self(), Message]), | |
NextPid ! {message, Message ++ World}, | |
world_node(NextPid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]) | |
end, | |
ok. | |
print_node() -> | |
receive | |
{message, Message} -> | |
io:format("Print Process (~p) receives message (~p)~n", [self(), Message]), | |
print_node(); | |
Message -> | |
io:format("Print Process (~p) receives unknown message (~p)~n", [self(), Message]) | |
end, | |
ok. | |
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(hello_world2). | |
-compile(export_all). | |
main() -> | |
HelloPid = spawn(fun() -> hello_node() end), | |
WorldPid = spawn(fun() -> world_node() end), | |
PrintPid = spawn(fun() -> print_node() end), | |
io:format("Send link messages~n"), | |
HelloPid ! {next, WorldPid}, | |
WorldPid ! {next, PrintPid}, | |
io:format("Sent messages~n"), | |
HelloPid ! {message, "I say"}, | |
ok. | |
hello_node() -> | |
receive | |
{next, Pid} -> | |
io:format("Hello Process (~p) linked to ~p~n", [self(), Pid]), | |
hello_node(Pid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]), | |
hello_node() | |
end, | |
ok. | |
hello_node(NextPid) -> | |
receive | |
{message, Message} -> | |
io:format("Hello Process (~p) receives message (~p)~n", [self(), Message]), | |
NextPid ! {message, Message ++ " Hello"}, | |
hello_node(NextPid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]), | |
hello_node(NextPid) | |
end, | |
ok. | |
world_node() -> | |
receive | |
{next, Pid} -> | |
io:format("World Process (~p) linked to ~p~n", [self(), Pid]), | |
world_node(Pid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]), | |
world_node() | |
end, | |
ok. | |
world_node(NextPid) -> | |
receive | |
{message, Message} -> | |
io:format("World Process (~p) receives message (~p)~n", [self(), Message]), | |
NextPid ! {message, string:concat(Message, " World")}, | |
world_node(NextPid); | |
Message -> | |
io:format("Hello Process (~p) receives unknown message (~p)~n", [self(), Message]), | |
world_node(NextPid) | |
end, | |
ok. | |
print_node() -> | |
receive | |
{message, Message} -> | |
io:format("Print Process (~p) receices message (~p)~n", [self(), Message]), | |
print_node(); | |
Message -> | |
io:format("Print Process (~p) receives unknown message (~p)~n", [self(), Message]) | |
end, | |
ok. |
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
.. | |
25> c(hello_world). | |
{ok,hello_world} | |
26> hello_world:main(). | |
Send link messages | |
Sent messages | |
Hello Process (<0.140.0>) linked to <0.141.0> | |
World Process (<0.141.0>) linked to <0.142.0> | |
Hello Process (<0.140.0>) receices message ("I say") | |
ok | |
World Process (<0.141.0>) receices message ("I say Hello") | |
Print Process (<0.142.0>) receices message ("I say Hello World") |
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(pattern). | |
-compile(export_all). | |
%% pattern matching | |
greet(male, Name) -> | |
io:format("Hello, Mr. ~s!~n", [Name]); | |
greet(female, Name) -> | |
io:format("Hello, Mrs. ~s!~n", [Name]); | |
greet(_, { FirstName, LastName }) -> | |
io:format("Hello, ~s ~s!~n", [FirstName,LastName]); | |
greet(_, Name) -> | |
io:format("Hello, ~s!~n", [Name]). | |
%% generic | |
head([H|_]) -> H. | |
second([_,X|_]) -> X. | |
%% bits | |
rgb(<<Red:8,Green:8,Blue:8>>) -> | |
{Red, Green, Blue}. | |
%% guards | |
old_enough(X) when X >= 16 -> true; | |
old_enough(_) -> false. |
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
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:8:8] [rq:8] [async-threads:0] [hipe] [kernel-poll:false] | |
Eshell V5.8.3 (abort with ^G) | |
1> c(pattern). | |
{ok,pattern} | |
2> pattern:greet(male,"Jack"). | |
Hello, Mr. Jack! | |
ok | |
3> pattern:greet(female,"Jill"). | |
Hello, Mrs. Jill! | |
ok | |
4> pattern:greet(alien,"Paul"). | |
Hello, Paul! | |
ok | |
5> pattern:greet(male,{"Tarn", "Barford"}). | |
** exception error: bad argument | |
in function io:format/3 | |
called as io:format(<0.25.0>,"Hello, Mr. ~s!~n",[{"Tarn","Barford"}]) | |
6> pattern:greet(whatever,{"Tarn", "Barford"}). | |
Hello, Tarn Barford! | |
ok | |
7> pattern:head([1,2,3]). | |
1 | |
8> pattern:second([1,2,3]). | |
2 | |
9> pattern:rgb(<<16#FF00FF:24>>). | |
{255,0,255} | |
10> pattern:old_enough(10). | |
false | |
11> pattern:old_enough(20). | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment