====== Erlang Intro Lab, cont. ======
Passing command line arguments, list manipulation, list comprehension, higher order functions.
In case of trouble take a look at: http://www.erlang.org
===== Running Erlang from Command Line and Redirection =====
Consider your ''go/0'' function from the previous lab: [[lab_erlang_1#File Reading]].
Run Erlang from the command line and tell it to call your ''go/0'' function.
Redirect the standard output to a file: ''output.dat''.
Is it identical to {{ac_current.dat}}?
Use the following function and save it in ''cmd'' module:
go(Arg) -> erlang:display(Arg), init:stop(1).
Run ''cmd:go/1'' from the command line (terminal) by issuing the following command:
erl -noshell -s cmd go arg1 12 5
Is there anything displayed?
* Explanation: any arguments passed to an erlang function from the operating system are available as a list of atoms.
Update your module implementing ''factorial/1'' to include ''disp_fact/1'' function which calculates factorial for all elements of the list given as its argument. Make it capable of running from the command line (mind that the command line arguments are passed as atoms) i.e.:
erl -noshell -s fact disp_fact 3 2 10 -s erlang halt
displays:
[6,2,3628800]
Hints:
- make ''factorial/1'' to calculate results from arguments given as a list,
- assume that this is a list of atoms,
- define ''disp_fact(L) -> erlang:display(factorial(L)).''.
===== More lists =====
Test the following program which finds Pythagorean Triangles.
Mind that with greater problem size the calculation process might take considerable time.
pythag(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B =:= C*C
].
Do you understand how the program works?
Using ''timer'' module measure how long it takes to calculate: ''pythag(100)''.
Write a function, which works in similar way as ''timer:tc/3'' but returns just a single value which is the elapsed time.
There is a function ''os:getenv/0'' which returns some information about the underlying OS in a form of pairs: ''attribute = value''.
Run it and see what it returns.
There is one particular attribute which indicates the name of the user which runs ''erl''. Its name is ''USER''.
Write a function which uses ''os:getenv/0'' and extracts information about current user by returning his name.
Use the list comprehension and pattern matching.
The result of running your function should be identical to ''os:getenv("USER")'' (oh yes, such a function already exists).
* Hint: use ''[$U,$S,$E,$R,$=|T]'' (or ''lists:split/2'' and optionally ''case ... of ...'' statement)
===== Higher Order Functions =====
Write a function which uses ''os:getenv/0'' and extracts information about current user by returning his name.
Use the higher order function approach this time.
The result of running your function should be identical to ''os:getenv("USER")''.
* Hint: use ''lists:filter/2''
There is some data coming from sensors in a form of pairs: ''{source,value}''. There are always three pairs in the record however they may come in different order.
Sample data:
[{current,33}, {speed,12}, {temperature,56}, {current,34}, {temperature,57}, {speed,13}, {current,28}, {speed,10}, {temperature,58}]
Write a function ''overload/2'' which returns ''true'' if any current value exceeds some given value and ''false'' otherwise, i.e.:
> L=[{current,33}, {speed,12}, {temperature,56}, {current,34}, {temperature,57}, {speed,13}, {current,28}, {speed,10}, {temperature,58}].
> mymod:overload(33, L).
true
> mymod:overload(40, L).
false
Write a function ''process/2'' which process the above data and indicates if there is an overload by replacing ''{current,X}'' with ''{current,X,overload}'' or ''{current,X,ok}'' if there is overload or not respectively.
The threshold value for the overload should be given as the first argument, i.e.:
> mymod:process(33, L).
[{current,33,ok}, {speed,12}, {temperature,56}, {current,34,overload}, {temperature,57}, {speed,13}, {current,28,ok}, speed{10}, {temperature,58}]
Write a function which displays the above data in a human readable form on the standard output, use ''io:format/2'':
current: 33
speed: 12
temperature: 56
current: 34
...