As an exercise in learning Erlang, I'm trying to write a simple database (from O'Reilly's Programming Erlang).
Basically I have a list of Tuples like this:
Db1 = [{person1,charleston},{person2,charleston},{person3,chicago}].
I need to create function such that
db:match(charleston,Db1).
returns
[person1,person2]
Here's the method that I wrote:
match(Element, Db) -> match(Element, Db, []).
match(_Element,[], Results) -> Results;
match(Element, [{Key,Value}|T], Results) ->
case Value == Element of
true -> match(Element, T, [Results,Key]);
false -> match(Element,T,Results)
end.
The result I'm getting back is this:
[[[],person1],person2]
I know there are ways of combining lists with the lists.erl module, but I'm trying to bypass it in an effort to learn more about the language. Any ideas what I'm doing wrong?
case Value == Elementcan be folded into the clause:match(Value, [{Key, Value}|T], ...) ->and so on. - I GIVE CRAP ANSWERS[Key|match(Element, T)]as a recurser. - I GIVE CRAP ANSWERS