I have written a simple function named is_zero
in Erlang which checks if the argument to the function is zero. The code is as follows.
-module(match).
-export([is_zero/1]).
% defining a function named is_zero
% this is a function with two clauses
% these clauses are matched sequentially
is_zero(0) ->
true;
is_zero(X) ->
false.
As I try to compile the code using c(match).
(the file is also named as match.erl) it returns a warning saying variable "X" is unused
.
If I run is_zero(0).
in spite of the warning, the shell throws an exception error saying undefined shell command is_zero/1
What am I doing wrong?
I cannot think of any rectification neither can I find any advice that will help.
Functions in Erlang are defined inside modules. To distinguish functions from different modules, you need to prepend the module name to a function. In this case, you need to run match:is_zero(0).
Instead of just is_zero(0).
.
To avoid the warning saying variable "X" is unused
, use the underscore variable:
is_zero(_) ->
false.
Note that variable names can be useful even if unused, to improve readability. If you want to name your variable and still avoid the compiler warning, prepend the underscore to the name:
is_zero(_Num) ->
false.