Getting Started with Erlang Language Learning

I recently researched RabbitMQ and found that it is based on Erlang, so I became interested in Erlang, the official website address http://www.erlang.org/, to download the latest version, I downloaded version 5.9.1, Windows version R15B01 Windows Binary File (90.7 MB), after downloading it, unzip and install it all the way. You don't need to do any separate settings, the Erlang language can be used, and then you can start programming with the Eshell program in it.

This is a command line program, you can directly enter expressions in it for calculation, for example, a simple:

quote
Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0]

Eshell V5.9.1  (abort with ^G)
1> 100+300.
400
2>


Note that you enter "." at the end of the statement to indicate completion, and press Enter to start calculating the result, as shown above.

Next, we use Notepad to write a factorial Erlang program:

quote
-module (test).
-export ([fac / 1]).

fac (0) -> 1;
fac (N) -> N * fac (N-1).


This program defines a module called "test" that contains a function called fac that takes 1 parameter.

According to Erlang's convention, the file name must be the same as the module name, so we save this file as "test.erl", and save the file in the D:\test1 directory.

The question is, how to run this file? Using the cd method, change Erlang's current working path:

quote
2> cd("d:/test1").
d:/test1
ok
3>


Then compile the program file just now:

quote
ok
3> c(test).
{ok,test}
4>


Note that the parameter of the compilation command c can be written with the name of the module defined in the file, and double quotation marks are not required. After compilation, the file "test.beam" will be generated under the current working directory.

Then, the methods in the module can be executed:

quote
4> fac(10).
** exception error: undefined shell command fac/1
5> test:fac(10).
3628800
6>


Note that a custom "external module" is used here, so you need to specify the module name: method name (parameter) when executing.



What if the compiled program needs to be reloaded next time?

It's very simple, just use l (module name), as in the following example:

quote
6> l(test).
{module,test}
7> test:fac(4).
24
8>


So far, the whole process of installing, writing, compiling, running, and loading an Erlang language program is completed. The entry process is still very simple, right?

Reference: http://www.cnblogs.com/bluedoctor/archive/2012/09/03/2668053.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326891832&siteId=291194637