Created
November 7, 2012 07:56
-
-
Save ptrelford/4030097 to your computer and use it in GitHub Desktop.
Erlang sequential programming exercises
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(crossring). | |
-compile(export_all). | |
start(N) -> | |
Pid = spawn(crossring,first,[N]), | |
register(cring, Pid), | |
ok. | |
send(M, Msg) -> cring ! {1, M, Msg }, ok. | |
first(N) -> | |
Next1=spawn(crossring,next,[(N div 2)+1,N,self()]), | |
First=spawn(crossring,loop,[1,Next1]), | |
Next2=spawn(crossring,next,[2,N div 2,First]), | |
loop(1,Next2). | |
next(N,N,First) -> | |
loop(N,First); | |
next(X,N,First) -> | |
Next=spawn(crossring,next,[X+1,N,First]), | |
loop(X,Next). | |
loop(N,Next) -> | |
receive | |
{ M, M, _ } -> | |
io:format("Done",[]), | |
loop(N,Next); | |
{ X, M, Msg } -> | |
io:format("~w ~s~n",[N,Msg]), | |
Next ! { X + 1, M, Msg }, | |
loop(N,Next) | |
end. |
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(my_db). | |
-compile(export_all). | |
start() -> | |
Db = [], | |
register(my_db, spawn(my_db,loop,[Db])), | |
ok. | |
call(Message) -> | |
my_db ! {request, self(), Message}, | |
receive | |
{ reply, Response } -> Response | |
end. | |
stop() -> my_db ! stop. | |
write(Key,Element) -> call({write, Key, Element}). | |
delete(Key) -> call({delete, Key}). | |
read(Key) -> call({read, Key}). | |
match(Element) -> call({match, Element}). | |
reply(Client, Msg) -> Client ! { reply, Msg }. | |
loop(Db) -> | |
receive | |
{ request, Client, {write, Key, Element} } -> | |
reply(Client,ok), | |
loop([{Key,Element}|Db]); | |
{ request, Client, {delete, Key, Element} } -> | |
reply(Client,ok), | |
loop(lists:keydelete(Key,1,Db)); | |
{ request, Client, {read, Key} } -> | |
case lists:keyfind(Key,1,Db) of | |
{_,Value} -> reply(Client, Value); | |
false -> { error, instance } | |
end, | |
loop(Db); | |
{ request, Client, {match, Element} } -> | |
Keys = [Key||{Key,Value}<-Db, Value=:=Element], | |
reply(Client, Keys), | |
loop(Db); | |
stop -> ok | |
end. |
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(ring). | |
-export([start/1,send/2,first/1,next/3,loop/1]). | |
start(N) -> | |
Pid = spawn(ring,first,[N]), | |
register(pring, Pid), | |
ok. | |
send(M, Msg) -> pring ! {1, M, Msg }, ok. | |
first(N) -> | |
Next=spawn(ring,next,[2,N,self()]), | |
loop(Next). | |
next(N,N,First) -> | |
loop(First); | |
next(X,N,First) -> | |
Next=spawn(ring,next,[X+1,N,First]), | |
loop(Next). | |
loop(Next) -> | |
receive | |
{ M, M, _ } -> | |
io:format("Done",[]); | |
loop(Next). | |
{ X, M, Msg } -> | |
io:format("~s~n",[Msg]), | |
Next ! { X + 1, M, Msg }, | |
loop(Next) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment