[erlang-questions] how to add a tuple to a list.

Richard A. O'Keefe ok@REDACTED
Fri Jan 30 04:02:03 CET 2015


On 30/01/2015, at 8:14 am, Roelof Wobben <r.wobben@REDACTED> wrote:

> e@REDACTED schreef op 29-1-2015 om 20:06:
>>> So i have to make a tuple of the data and add it in a list. 
>>> 
>>> I thought I could do something like this : 
>>> 
>>> write(Key, Data, Db) -> 
>>>    [ {key, data} | Db ] 
>>> 
>>> but then I see some error messages. 
>> 
>> what messages? 
>> 
>> as far as i can _theorize_ 
>> the most probably your Db is not a list, and it should be. 
>> _______________________________________________ 
>> erlang-questions mailing list 
>> erlang-questions@REDACTED 
>> http://erlang.org/mailman/listinfo/erlang-questions 
>> 
> 
> Here is my code so far : 
> 
> -module(db).
> 
> -export([new/0, destroy/1]).
> 
> new() ->
>   [].
> 
> destroy(Db) ->
>   {ok}.
> 
> write(Key, Element, Db) ->
>    [ [{key, data}] | Db ] 
> 
> 
> and this the output of c(db).
> 
>                                                                                                                                                                                     
> 3> c(db).                                                                                                                                                                              
> db.erl:12: syntax error before:                                                                                                                                                        
> db.erl:8: Warning: variable 'Db' is unused                                                                                                                                             
> error   

The first message was telling you that there was a syntax error
at the very end.  There is.  You are missing the full stop “.” at
the end of the definition of write/3.

The second message was telling you that the variable “Db” is not
used in the body of destroy/1.  By the way, destroy/1 does nothing
useful and certainly does NOT destroy anything.

There is another error: you do not -export write/3.



Now it’s time for me to tell you about an efficiency issue.
Let’s start by adding another function:

-export([write/3, read/2]).

read(Key, [{Key,Datum]|_]) ->
    Datum;
read(Key, [_|Db]) ->
    read(Key, Db).

This will raise an exception if you call read/2 with a key that is
not present in Db.

Now let’s try a little loop.

test1() ->
    test1(1000000000, db:write(foo, 0, db:new())).

test1(0, Db) ->
    db:destroy(Db);
test1(N, Db) ->
    test1(N-1, db:write(foo, db:read(foo, Db) + 1, Db)).

This creates a data base with just one maplet foo:->0 and then
loops 1,000,000,000 times picking out the value associated with
foo, adding 1 to it, and writing that back.

What is finally passed to db:destroy/1 is
[{foo,1000000000},{foo,999999999},{foo,999999998},…]

For real practical use, you should think about using an
existing Erlang module like dict (http://www.erlang.org/doc/man/dict.html).
If you want to continue to treat this as a programming exercise,
then you should think about replacing existing records as well as inserting
new ones.  The simplest thing to do would be to use a naive simple binary
search tree.

% Empty tree: ‘empty’.
% Non-empty tree: {Key, Value, Less, Greater}.
%   all keys in Less < Key; all keys in Greater > Key.

lookup(Key, {K,V,L,R}, Default) ->
    if Key < K -> lookup(Key, L, Default)
     ; Key > K -> lookup(Key, R, Default)
     ; true    -> V
    end;
lookup(Key, empty, Default) ->
    Default.


lookup(Key, {K,V,L,R}) ->
    if Key < K -> lookup(Key, L)
     ; Key > K -> lookup(Key, R)
     ; true    -> V
    end.





More information about the erlang-questions mailing list