Scatman_John
Scatman_John

Reputation: 107

no function clause matching erlang

I just picked up Erlang and I ran into a simple problem, but I have not been able to fix it or find anything about it. I'm trying to define a module that checks if an atom is in a given list. I entered the list through the Erlang shell like this:

veggies:veggieMember([cucumber,tomato,potato],tomato).

But I always get exception error: no function clause matching

Maybe I misunderstood the basics, but here is the module code I'm trying to do:

-module(veggies).

-export([veggieMember/2]).

veggieMember(veggieList,query)->
case lists:member(query, veggieList) of
    true->veggieList;
    false->[query|veggieList]
end.

Upvotes: 3

Views: 10656

Answers (1)

Łukasz Ptaszyński
Łukasz Ptaszyński

Reputation: 1689

Binding in erlang starts with a capital letter. So it should be:

-module(veggies).

-export([veggieMember/2]).

veggieMember(VeggieList,Query)->
case lists:member(Query, VeggieList) of
    true -> VeggieList;
    false -> [Query|VeggieList]
end.

In your example it didn't work because there is no matching function clause. Atom veggieList doesn't match list [cucumber,tomato,potato] and atom query doesn't match atom tomato.

The error itself, it one of the standard errors. It means you have made a call to function and that none of the function clauses (separated by ;) matched.

Upvotes: 11

Related Questions