Reputation: 1047
I have created a simple program that should simulate an address book:
%% API
-export([]).
-export([createAddressBook/0]).
-export([checkIfExist/3]).
-export([addContact/3]).
-record(entry,{nameAndSurname,n_phone,email}).
createAddressBook() ->
Test = #entry{nameAndSurname = {"Marcin", "Majewski"}, n_phone = [997], email=["[email protected]"]},
[Test].
checkIfExist(_,_,[]) -> false;
checkIfExist(Name,Surname,[H|L]) ->
if
H#entry.nameAndSurname == {Name,Surname} -> true;
true -> checkIfExist(Name,Surname,L)
end.
addContact(Name,Surname,L)->
case checkIfExist(Name,Surname,L) of
true -> io:format("Entry already exist, nothing was created!");
_ -> newEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email = []},lists:append(L,newEntry)
end.
But when I invoke:
X=module_name:createAddressBook().
B=module_name:addContact("Name","Surname",X).
I am getting an error:
** exception error: no match of right hand side value
{entry,{"Name","Surname"},[],[]}
in function addContact
I do not understand what causes this problem.
Upvotes: 0
Views: 78
Reputation: 1745
You are tryin to assign record to atom:
newEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email = []},lists:append(L,newEntry)
Since newEntry
starts from lowercase letter, erlang treats it as atom, but not variable. Just change it:
NewEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email]},
lists:append(L,NewEntry)
Upvotes: 2