[erlang-questions] wierd outome on my match method

Roelof Wobben r.wobben@REDACTED
Sat Jan 31 15:31:35 CET 2015


Thanks,

Everything is working now.
Finnaly I have solved the whole exercise like this :

-module(db).

-export([new/0, destroy/1, write/3, read/2, delete_key/3, delete/2, 
match/2, match_key/3]).

new() ->
   [].

destroy(_Db) ->
   ok.

write(Key, Element, Db) ->
    [ {Key, Element} | Db ].

read(_Key, []) ->
   {error, "Instance"};

read(Key, [Head | List]) ->
   case element(1, Head) of
     Key -> {ok, element(2, Head)};
     _ -> read(Key, List)
   end.

delete_key( _Key, [] , List2) ->
   List2 ;

delete_key( Key, [Head | Tail], List3 ) ->
   case element(1, Head) of
     Key -> delete_key(Key, Tail, List3 );
     _ -> delete_key(Key, Tail, [Head | List3] )
    end.

delete( _Key, [] ) ->
   {error, "Instance"};

delete(Key, List) ->
   match_key (Key, List, []).

match_key( _Key, [] , List) ->
   List ;

match_key( Key, [Head | Tail], List ) ->
   case element(1, Head) of
     Key -> match_key(Key, Tail, [element(2,Head) | List] );
     _ -> match_key(Key, Tail,  List )
    end.

match( Key, [] ) ->
   {error, "Instance"};

match(Key, List) ->
   match_key (Key, List, []).


Now I have 2 questions :

1) Is this a good solution.
2) Can I somewhere hide my helper functions (match_key, delete_key) and 
do not get a warning about unused.

Roelof




Loïc Hoguin schreef op 31-1-2015 om 15:23:
> On 01/31/2015 03:11 PM, Roelof Wobben wrote:
>> match( Key, [] ) ->
>>    {error, "Instance"};
>>
>> match(Key, List) ->
>>    delete_key (Key, List, []).
>
> This above is the function you call. It does not perform a match.
>
>> But when I have this database.
>>
>> Db5 :  [{name2,tamara},{name,chantal},{name,roelof}]
>>
>> and I do this :
>>
>>   Db11 = db:match(name,Db5).
>>
>> I see this outcome :
>>
>> [{name2,tamara}]
>>
>> and when I do this :
>>
>>   Db10 = db:match(name2,Db5).
>>
>> I see this outcome:
>>
>> [{name,roelof},{name,chantal}]
>>
>> So it schould be the other way around.
>>
>> Can someone help me figure out what went wrong ?
>>
>> Roelof
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>




More information about the erlang-questions mailing list