User Tools

Site Tools


cso:lab_erlang_1

Erlang Intro Lab

Introduction to Erlang. Using the Shell. Variables. Compilation and running of simple programs/functions. Running Erlang VM from the command line.

In case of trouble take a look at: http://www.erlang.org

Use Erlang as a calculator

To enter the Erlang shell run erl command from a terminal.

Calculate: 3+2*10 and (3+2)*10

What are the results? Does arithmetic operator precedence work?

Variables and the Shell

Repeat calculations from the previous exercise, but this time assign the results to variables:

O=3+2*10.
P=(3+2)*10.

What does the following command do?

O+P.

Try the following, does it work? Why?

P=0.

Using the shell internal commands (see help(). for help :-) ) investigate what variables are defined in your shell.

Delete (forget) variable P binding. Does the following work now? Why?

P=0.

Matching vs. Comparison

Try the following:

2=2.

What is the result? And now:

2==2.

Is there a difference why?

  • Explanation: matching is different than comparison. Matching returns the matched value, comparison an atom true or false.

Try the following and observe the results.

3+1 = 2+2.
2*3 == 5+1.
4/2 =:= 1+1.
5 rem 3.
5 div 3.
5/3 = 15/9.
5/3 =:= 15/9.
5/3 == 15/9.
1.0 + 3 =:= 2+2.
1.0 + 3 == 2+2.
1.0 + 3 = 2+2.
1.0 + 3 = 2+2 + 0.0.

For more comparison operators see the manual at http://www.erlang.org

Simple functions: Factorial

Create a file my1.erl with a factorial function.

Use your favorite text editor. Remember about the compiler directives -module(). and -export(). You may also use -compile(export_all). feature for now.

Run erl in the same directory as my1.erl Compile my1.erl

c(my1).

Test if the factorial function works.

Is factorial(400) doable? Can your pocket calculator do that? Experiment with some greater numbers.

Factorial(-5) does it work? Did you use guards while implementing the function?

If your program entered an infinite loop (it is not respoding) use one of the methods from the lecture to terminate it: Use ^G, investigate different user shell commands

  • to get help press 'h'
  • to list jobs (processes) press 'j'
  • to terminate the job press 'i' followed by optional job number
  • to get back to the shell, connect to its process (job) with 'c'

You can experiment with the user shell commands i.e. you are free to start as many shell as you like and switch among them.

Leave erl by issuing:

q().

Enter erl again.

Try running the factorial function without prior compilation. Does it work? It should!

  • Explanation: Erlang would load requested modules on-demand if they are compiled.

Lists

Investigate the following matches:

[F,S|Rest] = [a,b,c,1,2,3].

What is the value of: F, S, Rest?

Try the following:

A=1.
L=[A|[2,3]].
[[3,2]|1].

Does the following match?

[H1|T1]=[].
[H2|T2]=[{salary,{fred,18000}}].

What are the values of: H1, T1, H2, T2?

Suppose that you want to extract information about somebody's salary. Assuming that H2 is already bound to the tuple describing it try the following:

{_,{_,S}}=H2, S.

What is the returned value? Why? Is S bound?

Write a function ml/1 which returns the greatest number on the list. Do not use max(L) BIF!

  • Hint: use recursion and '|' operator.

File Reading

Here is a simple program which implements a function get_data/1 which reads data from a file. The file's name is given as its argument. The data in the file is given as floating point numbers separated with new lines. process/1 displays data on the standard output, the numbers are separated with commas.

Read the code. Is it clear to you? Test it on the data file from the previous exercise. Does it work?

get_data(Name) ->
    case file:open(Name,[read]) of 
        {ok,FD} ->
            Val=process(FD),
            file:close(FD),
            Val;
        Error -> Error
end.

process(FD) ->
    case io:fread(FD,'',"~f") of
        {ok,Value} -> io:format("~f, ",Value),
                      process(FD);
        Err -> Err
end.

Change process/1 to display the numbers line by line.

Rewrite process/1 not using the case … of … statement.

Write a function go/0 which reads data from ac_current.dat using get_data/1.

cso/lab_erlang_1.txt · Last modified: 2021/01/08 14:10 (external edit)