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

Tony Rogvall tony@REDACTED
Fri Jan 30 15:48:15 CET 2015


Hi Roelof!

Have you ever thought about picking up a programming book?

There is a very good one that is free and you can read it online.

Here is a link: http://learnyousomeerlang.com/content

Have fun.

/Tony


> On 30 jan 2015, at 08:15, Roelof Wobben <r.wobben@REDACTED> wrote:
> 
> Rick Pettit schreef op 29-1-2015 om 22:49:
>> Roelof,
>> 
>> You don’t need a variable if you don’t care about the return value of the function you are calling.
>> 
>> To be clear, your problem with the badmatch is due to Db2 already having a value assigned which is not equal to the atom ‘ok’.
>> 
>> Try this at the shell to see what that value is:
>> 
>>     Db2.    % don’t forget the period there
>> 
>> More than likely it is set to {ok}, which is what you previously had db:destroy/1 returning.
>> 
>> Since erlang is a single-assignment language, you cannot simply assign a different value to Db2 once it is bound.
>> 
>> You *can* use syntax that appears just like an assignment and have that work, provided the value returned from the function matches exactly the value currently bound to the variable on the left-hand side of the “=“.
>> 
>> For example:
>> 
>> Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
>> 
>> Eshell V6.2  (abort with ^G)
>> 1> Db2 = {ok}.
>> {ok}
>> 2> Db2.
>> {ok}
>> 3> Db2 = {ok}.
>> {ok}
>> 4> Db2 = ok.
>> ** exception error: no match of right hand side value ok
>> 5> Db2 = {ok}.
>> {ok}
>> 6> f(Db2).
>> ok
>> 7> Db2 = ok.
>> ok
>> 8> Db2 = {ok}.
>> ** exception error: no match of right hand side value {ok}
>> 9> Db2 = ok.
>> ok
>> 
>> ===
>> 
>> Here’s what’s going on there:
>> 
>> 1. I bound the value {ok} to the variable Db2
>> 
>> 2. I check the value of Db2
>> 
>> 3. I “assign” Db2 the same value — this isn’t really an assignment now, it is a match, and in this case both the left-hand side and right-hand side have same value, so match succeeds
>> 
>> 4. I attempt to assign Db2 a different value—since Db2 was bound to {ok} and the right-hand side now has ‘ok’, that match fails—thus the match error
>> 
>> 5. I “assign” Db2 the original value — again, this isn’t really an assignment, it is a match, which again succeeds for same reason as (3) above
>> 
>> 6. I use the f() function at the shell to “forget” about the value previously bound to Db2, effectively making Db2 unbound to any value (and as such it can now be assigned any value)
>> 
>> 7. I assign a different value, this time ‘ok’, to Db2 — no errors that time, because it was an assignment, not a match
>> 
>> 8. I attempt to match Db2 with the previous value—and there’s that match error again
>> 
>> 9. I “assign” Db2 the new value again—now it is a match, and the match is good so no error
>> 
>> 
>> Does that make sense now?
>> 
>> -Rick
>> 
>>> On Jan 29, 2015, at 1:53 PM, Roelof Wobben <r.wobben@REDACTED <mailto:r.wobben@REDACTED>> wrote:
>>> 
>>> I did some trail and error and saw this :
>>> 
>>> c(db).
>>> {ok,db}
>>> 14> Db1 = db:new().
>>> []
>>> 15> Db2 = db:destroy(Db1).
>>> ** exception error: no match of right hand side value ok
>>> 16> db:destroy(Db1).
>>> ok
>>> 
>>> So it seems you do not have to use a variable when destroying.
>>> 
>>> Roelof
>>> 
>>> 
>>> 
>>> Rick Pettit schreef op 29-1-2015 om 20:40:
>>>> Oops, meant to add that as for the exception error no match of right hand side value ok, is it possible you previously bound the variable Db2 at the shell?
>>>> 
>>>> If so, then though what you have appears to be an assignment, it is actually a match, and one which has failed because Db2 has been bound to a value other than ‘ok’.
>>>> 
>>>> You can do the following if you want the shell to “forget” about Db2’s previous value, effectively unbinding it and allowing for a subsequent assignment to a different value:
>>>> 
>>>>     f(Db2).
>>>> 
>>>> -Rick
>>>> 
>>>>> On Jan 29, 2015, at 1:38 PM, Rick Pettit <rpettit@REDACTED <mailto:rpettit@REDACTED>> wrote:
>>>>> 
>>>>> Your db:destroy/1 function doesn’t use the parameter passed in—thus the error about ‘Db’ being unused.
>>>>> 
>>>>> Since nothing is actually being “destroyed” there, you have a couple options:
>>>>> 
>>>>>   (1) modify destroy so it takes no arguments (kind of pointless—might as well do away with the function altogether)
>>>>> 
>>>>>   (2) rewrite the function head like so to inform the compiler you don’t care to use Db in the function body:
>>>>> 
>>>>> destroy(_Db) ->
>>>>>   ok.
>>>>> 
>>>>> 
>>>>> 
>>>>> -Rick
>>>>> 
>>>>>> On Jan 29, 2015, at 1:25 PM, Roelof Wobben <r.wobben@REDACTED <mailto:r.wobben@REDACTED>> wrote:
>>>>>> 
>>>>>> Rick Pettit schreef op 29-1-2015 om 20:16:
>>>>>>> Comments inline below.
>>>>>>> 
>>>>>>> -Rick
>>>>>>> 
>>>>>>>> On Jan 29, 2015, at 1:14 PM, Roelof Wobben <r.wobben@REDACTED <mailto:r.wobben@REDACTED>> wrote:
>>>>>>>> 
>>>>>>>> e@REDACTED <mailto: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 <mailto:erlang-questions@REDACTED>
>>>>>>>>> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
>>>>>>>>> 
>>>>>>>> 
>>>>>>>> Here is my code so far :
>>>>>>>> 
>>>>>>>> -module(db).
>>>>>>>> 
>>>>>>>> -export([new/0, destroy/1]).
>>>>>>>> 
>>>>>>>> new() ->
>>>>>>>>   [].
>>>>>>>> 
>>>>>>>> destroy(Db) ->
>>>>>>>>   {ok}.
>>>>>>> 
>>>>>>> Why are you returning a tuple here instead of simply ‘ok’ ?
>>>>>>> 
>>>>>> 
>>>>>> Because of this output :
>>>>>> 
>>>>>> 10> c(db).
>>>>>> db.erl:8: Warning: variable 'Db' is unused
>>>>>> {ok,db}
>>>>>> 11> Db1 = db:new().
>>>>>> []
>>>>>> 12> Db2 = db:destroy(Db1).
>>>>>> ** exception error: no match of right hand side value ok
>>>>>> 
>>>>>> When I do this :
>>>>>> 
>>>>>> -module(db).
>>>>>> 
>>>>>> -export([new/0, destroy/1, write/3]).
>>>>>> 
>>>>>> new() ->
>>>>>>   [].
>>>>>> 
>>>>>> destroy(Db) ->
>>>>>>   ok.
>>>>>> 
>>>>>> write(Key, Element, Db) ->
>>>>>>    [ {Key, Element} | Db ].
>>>>>> 
>>>>>> Roelof
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> _______________________________________________
>>>>>> erlang-questions mailing list
>>>>>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>>>>> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
>>>>> 
>>>> 
>>> 
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
>> 
> 
> Thanks all, this problem is also solved.
> 
> Roelof
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150130/b162c2dc/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150130/b162c2dc/attachment.bin>


More information about the erlang-questions mailing list