[erlang-questions] Where is the syntax error.
Richard A. O'Keefe
ok@REDACTED
Fri Sep 25 04:20:46 CEST 2015
On 25/09/2015, at 4:05 am, Roelof Wobben <r.wobben@REDACTED> wrote:
> elapsed_time(F) ->
> Begintime = erlang:monotonic_time(),
> F(),
> Endtime = erlang:monotonic_time(),
...
Here you are calling F with *NO* arguments.
> time:elapsed_time((Answer = fun(x) -> x + 2 end )2).
There are four things wrong with that.
(1) What's with end)2)? What's the 2 doing there?
(2) You have written a function of ONE argument, but
the module wants a function of NO arguments.
Writing 'x' as the argument is not an error in itself,
but you will seldom have reason to write a function
that accepts *only* the literal atom 'x' as argument
and nothing else.
(3) x + 2 cannot possibly work. x is an atom. Erlang
has no idea what arithmetic on atoms might mean.
You *probably* meant fun (X) -> X + 2 end.
(4) You have Answer = ... which is not a syntax error
but is both pointless (because the variable is
never used again) and confusing (because the value
that is bound to the variable is NOT the answer to
any question you're asking).
> time:elapsed_time(fun () -> 2 end).
is what you want.
Except that a trivial function like that is not going to
consume enough time for you to measure it. Try something
slower, like opening and closing an existing file.
More information about the erlang-questions
mailing list