[erlang-questions] need some help with erlang

Dawid Figiel dawid.figiel@REDACTED
Tue Feb 16 19:48:18 CET 2010


----- Original Message -----
From: "Dale Harvey" <dale@REDACTED>
To: "Sarvar Muminov" <sarvar.muminov@REDACTED>
Cc: erlang-questions@REDACTED
Sent: Tuesday, February 16, 2010 6:39:47 PM GMT +00:00 GMT Britain, Ireland, Portugal
Subject: Re: [erlang-questions] need some help with erlang

a few things wrong with this

On 16 February 2010 18:13, Sarvar Muminov <sarvar.muminov@REDACTED> wrote:

> I wrote this small db emulator to store the data in a hash.
> I used "write" function to write data to the hash. But when I try to add
> some data with key, value, It didn't work.
> I suppose I made some mistake in the code, but I can't figure out the
> problem :)
>
> -module(db_server).
>
> -export([start/0, init/1, write/2, read/1]).
>
> start() ->
>    register(server, spawn(db_server, init, [dict:new()])).
>
> init(Records) ->
>    receive
>        {add, Key, Value} ->
>            dict:store(Key, Value, Records),
>            {ok, Key, Value};
>        {show, Key} ->
>

When you compile you should get warning that a term is constructed and never
used,
you arent doing anything with it, you can io:format it out, typically you
would send the server
your own pid with self() so it can send the value back


>            case dict:find(Key, Records) of
>                {ok, Value} -> Value;
>                error -> {error, no_such_value}
>            end;
>        {delete, Key} ->
>            dict:erase(Key, Records),
>            ok
>    end,
>

You are calling the loop function with the same variable it as passed in,
which was dict:new(), so it will
always be a new (empty) dict, when you modify the dict you need to call it
with the new version


>    init(Records).
>
> write(Key, Value) ->
>    server ! {add, Key, Value}.
>
> read(Key) ->
>    server ! {show, Key}.
>



Hi Dale:)



I have fixed your example, see below:

-module(db_server).

-export([start/0, init/1, write/2, read/1, delete/1]).

start() ->
    register(server, spawn(db_server, init, [dict:new()])).

init(Records) ->
    receive
        {add, Pid, Key, Value} ->
            RecordsNew = dict:store(Key, Value, Records),
            Pid ! {ok, Key, Value},
            init(RecordsNew);
        {show, Pid, Key} ->
            case dict:find(Key, Records) of
                {ok, Value} -> Pid ! {ok, Value};
                error ->  Pid ! {error, no_such_value}
            end,
            init(Records);
        {delete, Pid, Key} ->
            RecordsNew = dict:erase(Key, Records),
            Pid ! {ok, ok},
            init(RecordsNew)           
    end.

write(Key, Value) ->
    server ! {add, self(), Key, Value},
    receive Res ->
            Res
    end.

read(Key) ->
    server ! {show, self(), Key},
    receive Res ->
            Res
    end.

delete(Key) ->
    server ! {delete, self(), Key},
    receive Res ->
            Res
    end.


Remember also that the return value is always the last value in the function. In the previous version it was i.e {add, Key, Value}

Very nice example to understand basic in erlang:)

--
best,


Dawid Figiel




More information about the erlang-questions mailing list