Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang atomic type


May 13, 2021 Erlang


Table of contents


Atomic type

The atomic type is another data type in the Erlang language. A ll atomic types begin with lowercase letters (see Atomic Types). F or example, charles, centimeter, inch, etc. A tomic types are just names, with no other meaning. T hey are different from variables, which have values, whereus atomic types do not.

Enter the following program into file tut2.erl. T his procedure completes the conversion between inches and centimeters:

-module(tut2).
-export([convert/2]).

convert(M, inch) ->
    M / 2.54;

convert(N, centimeter) ->
    N * 2.54.

Compile:

9> c(tut2).
{ok,tut2}

Test:

10> tut2:convert(3, inch).
1.1811023622047243
11> tut2:convert(7, centimeter).
17.78

Note that so far we haven't covered the number of points. I hope you'll find out for a little time.

Let's see what happens if the input parameters are neither centimeter nor inch:

12> tut2:convert(3, miles).
** exception error: no function clause matching tut2:convert(3,miles) (tut2.erl, line 4)

The two parts of the convert function are called the two clauses of the function. A s you can see, miles are not part of the clause. T he Erlang system could not find a matching clause, so an error message was returned function_clause. T he shell is responsible for the error message-friendly output, while the error group is stored in the shell's history list, which can be output using the v/1 command:

13> v(12).
{'EXIT',{function_clause,[{tut2,convert,
                                [3,miles],
                                [{file,"tut2.erl"},{line,4}]},
                          {erl_eval,do_apply,5,[{file,"erl_eval.erl"},{line,482}]},
                          {shell,exprs,7,[{file,"shell.erl"},{line,666}]},
                          {shell,eval_exprs,7,[{file,"shell.erl"},{line,621}]},
                          {shell,eval_loop,3,[{file,"shell.erl"},{line,606}]}]}}

The metagroup

The style of the previous tut2 program is not a good programming style. F or example:

tut2.convert(3,inch)  

Does this mean that 3 itself is already indicated by inches? O r convert 3 cm into inches? E rlang provides a mechanism for grouping certain elements and representing them in a more understandable way. I t's a metagroup. A metagroup is enclosed in parentheses.

So, the point is 3 inches, and the point of 5 centimeter, 5, is 5 centimeters. N ext, we'll rewrite the conversion program between centimeters and inches. E nter the following code into the file tut3.erl file:

-module(tut3).
-export([convert_length/1]).

convert_length({centimeter, X}) ->
    {inch, X / 2.54};
convert_length({inch, Y}) ->
    {centimeter, Y * 2.54}.

Compile and test:

14> c(tut3).
{ok,tut3}
15> tut3:convert_length({inch, 5}).
{centimeter,12.7}
16> tut3:convert_length(tut3:convert_length({inch, 5})).
{inch,5.0}

Note that line 16 code converts 5 inches to centimeters and then inches, so it gets the original value. T his also indicates that one function argument can be the return result of another function. T ake a closer look at how line 16 of code works. A fter passing the argument s inch, 5, to the convert_length function is matched convert_length({inch,5}) is matched. Y ou can also think of it as a success that the previous content of the .centimeter, X, did not match the .inch, 5' (the "-" A fter the first match fails, the program tries the second convert_length({inch,5}) The second statement matched successfully, so the Y value is 5.

There can be more elements in the group than just two parts, as described above. I n fact, you can use as many parts as you want in a metagroup, as long as each part is a legitimate Erlang item. F or example, represents the temperature values of different cities around the world:

{moscow, {c, -10}}
{cape_town, {f, 70}}
{paris, {f, 28}}

Each of these groups has a fixed number of items. E ach item in a metagroup is called an element. T he first element is moscow and the second element is . Where c represents Celsius and f represents Fahrenheit.