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

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


On 30/01/2015, at 8:04 am, Roelof Wobben <r.wobben@REDACTED> wrote:
> Db1 = db:write(francesco, london, Db).
> [{francesco,london}]

I presume the horrible name is part of the requirements you are
working from, not something you came up with.  In any programming
language, procedure names that are imperative verb (phrases)
should be reserved for things that have side effects, which this
does not.

There is a difference between
 - INSERTING a record for a NEW key into a data base
 - REPLACING a record for an EXISTING key into a data base
 - setting a record for a key that might or might not already exist.

You should start by writing a comment.

%   write(Key, Data, DB0) -> DB1
%   ensures that DB1 maps Key to Data and every other key that
%   is in DB0 to the same value it has in DB0.

> So i have to make a tuple of the data and add it in a list.

This is a very imperative way of talking and is likely to mislead
you.  You cannot add anything to a list in Erlang.  Once you have
a list, that is what it is, forever.  You can create a *new* list
that has the old one as a tail, but the old list is not changed.

> 
> I thought I could do something like this :
> 
> write(Key, Data, Db) ->
>  [ {key, data} | Db ]
> 
> but then I see some error messages.

It would help enormously if you would say WHAT error messages.

I notice that you have Key, Data - - these are variables - -
in the argument list but key, data - - these are constants - -
in the tuple.  Presumably you meant to write

write(Key, Data, DB) ->
    [{Key,Data} | DB].

Like C, C++, C#, Java, JavaScript, XML, . . ., alphabetic case
matters in Erlang.  “Key” and “key” are not the same term.




More information about the erlang-questions mailing list