Exercise 3-4 Database Handling Using Lists

Here’s my clumsy attempt at the database handling exercise. Man, I must be a nerd, but I actually like these exercises.. I’m also enjoying my biology class. Is there something wrong with me?

[code lang="erl"]-module(db).
-export([new/0, destroy/1, write/3, delete/2, read/2, match/2]).

new() ->
[].

destroy(Db) ->
ok.

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

delete(Key, [{DbHeadKey, DbHeadElement}|DbTail]) ->
case DbHeadKey of
Key -> delete(Key, DbTail);
_ -> [{DbHeadKey, DbHeadElement}] ++ delete(Key, DbTail)
end;
delete(Key, []) ->
[].

read(Key, [{DbHeadKey, DbHeadElement}|DbTail]) ->
case DbHeadKey of
Key -> {ok, DbHeadElement};
_ -> read(Key, DbTail)
end;
read(Key, []) ->
{error, instance}.

match(Element, [{DbHeadKey, DbHeadElement}|DbTail]) ->
case DbHeadElement of
Element -> [DbHeadKey] ++ match(Element, DbTail);
_ -> match(Element, DbTail)
end;
match(Element, []) ->
[].

[/code]

One thought on “Exercise 3-4 Database Handling Using Lists

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>