[erlang-questions] How to make this work

Richard A. O'Keefe ok@REDACTED
Thu Aug 13 02:46:15 CEST 2015


On 13/08/2015, at 3:34 am, Roelof Wobben <r.wobben@REDACTED> wrote:

> Hello,
> 
> Im trying this exercise from the programming erlang book.
> 
> Look up the definitions of erlang:now/0, erlang:date/0, and erlang:time/0.

These functions are documented in

http://www.erlang.org/doc/man/erlang.html

The documentation of erlang:date/1 does not say whether month
numbers are 0 origin (as a C programmer would expect) or 1
origin (as everyone else would expect) but experimentation
will tell you that.

Of course, having separate date() and time() functions is
<polite>about as far as you can get from a good idea</polite>
so we should be grateful that erlang:localtime() exists and
from the tablets of our memory erase all trivial fond records
of date() and time().

The documentation for erlang:now(), as you may have noticed,
has a big red Warning saying "This function is deprecated!
Do not use it!"  There is a link to
http://www.erlang.org/doc/apps/erts/time_correction.html#Dos_and_Donts
to tell you what to use instead.

> Write a
> function called my_time_func(F), which evaluates the fun F and times how
> long it takes.
> 
> So I did this :
> 
> -module(my_time).
> 
> -export( [time_spend/1] ).
> 
> time_spend(F) ->
>    Begintime = time:now(),
>    F(3),
>    Endtime = time:now(),
>    Endtime - Begintime.

First, the étude said to call the function "my_time_func"
and you have called it "time_spend".  Second, that is not
grammatical English.  It should be "time_spent" with a "t".
Third, you were told about a function ERLANG:now(), so
why are you calling TIME:now()?
And fourth, you DIDN'T read the documentation for erlang:now().
It returns a TUPLE, not a number.
The subtraction operator works only on NUMBERS, not tuples.

Life is too short not to Read The Fine Manual.

> 
> 
> but then when I do this in erl :
> 
> 11> my_time:time_spend(fun x -> 2 * X end).
> * 1: syntax error before: '->'

You have two errors here.
Error 1: the arguments of an Erlang fun are ALWAYS
enclosed in parentheses.  Always.
Error 2: you have 'x' in the argument position but
'X' when you use it.  This should be

11> my_time:time_spend(fun (X) -> 2*X end).

> 11> my_time:time_spend(Double = fun x -> 2 * X end).
> * 1: syntax error before: '->'
> 
> how can I make this work ?

What was the point of putting 'Double =' there?
Just use legal syntax for funs:

11> my_time:time_spend(fun (X) -> 2*X end).

By the way, do not expect the time for a function this
simple to be measurable.

Oh, there's an important issue here.
When you measure "the time" taken by a function,
what do you actually want to measure?
- Physical "wall clock" time?
- CPU time used by the Erlang node as a whole?
- CPU time used by the current scheduler?
- CPU time used by the current Erlang process?
(Hint: the one you want is not actually provided by
Erlang.  See the documentation of erlang:statistics/1.)

> 



More information about the erlang-questions mailing list