Can anyone tell me what this means? I am new to this and my friend recommended me to post in this website. By the way I'm new to Erlang.
If possible I want to write a code in editor and I don't even understand the question any sample input/output and how it works an explanation will do. Thankyou
Here's a simple example showing how to use function clauses, then case statements to do the same thing. Put the following code in a file named a.erl
in some directory:
-module(a).
-export([show_stuff/1, show_it/1]).
show_stuff(1) ->
io:format("The argument was 1~n");
show_stuff(2) ->
io:format("The argument was 2~n");
show_stuff(_)->
io:format("The argument was something other than 1 or 2~n").
show_it(X) ->
case X of
1 -> io:format("The argument was 1~n");
2 -> io:format("The argument was 2~n");
_ -> io:format("The argument was something other than 1 or 2~n")
end.
Note that the file name, a.erl
and the module directive:
-module(a).
must match. So, if you named your file homework1.erl
, then the module directive in the file must be:
-module(homework1).
To save a lot of typing, it's best to use very short module names (as you will see below).
In a terminal window, switch directories to the directory containing a.erl
:
~$ cd erlang_programs/
then launch the erlang shell:
~/erlang_programs$ erl
Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V12.0.2 (abort with ^G)
Next, execute the following statements:
1> c(a). <--- Compiles the code in your file
{ok,a} <--- Or, you may get errors which must be corrected, then try recompiling.
2> a:show_stuff(1).
The argument was 1
ok
3> a:show_stuff(4).
The argument was something other than 1 or 2
ok
4> a:show_it(1).
The argument was 1
ok
5> a:show_it(4).
The argument was something other than 1 or 2
ok
6>
Note the syntax for calling a function defined in a file/module:
module_name:function_name(arg1, arg2, ... argn).