How to make this work

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

How to make this work

Roelof Wobben-2
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. 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.


but then when I do this in erl :

11> my_time:time_spend(fun x -> 2 * X end).
  * 1: syntax error before: '->'
11> my_time:time_spend(Double = fun x -> 2 * X end).
* 1: syntax error before: '->'

how can I make this work ?

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Garry Hodgson-2
read the documentation on the proper syntax for defining funs.

On 8/12/15 11:34 AM, Roelof Wobben 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. 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.
>
>
> but then when I do this in erl :
>
> 11> my_time:time_spend(fun x -> 2 * X end).
>  * 1: syntax error before: '->'
> 11> my_time:time_spend(Double = fun x -> 2 * X end).
> * 1: syntax error before: '->'
>
> how can I make this work ?
>
> Roelof
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast
> antivirussoftware.
> https://www.avast.com/antivirus
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Garrett Smith-5
In reply to this post by Roelof Wobben-2
Joe, avert your eyes ;)

http://erlang.org/doc/programming_examples/funs.html#id59660

On Wed, Aug 12, 2015 at 10:34 AM, Roelof Wobben <[hidden email]> 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.
> 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.
>
>
> but then when I do this in erl :
>
> 11> my_time:time_spend(fun x -> 2 * X end).
>  * 1: syntax error before: '->'
> 11> my_time:time_spend(Double = fun x -> 2 * X end).
> * 1: syntax error before: '->'
>
> how can I make this work ?
>
> Roelof
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> https://www.avast.com/antivirus
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

dmkolesnikov
In reply to this post by Roelof Wobben-2
Hello,

You can use built-in function
http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1

You can also check from OTP source the implementation of this function and compare it with your's
https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160

It gives you hints :-)

Best Regards,
Dmitry

> On 12 Aug 2015, at 18:34, Roelof Wobben <[hidden email]> 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. 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.
>
>
> but then when I do this in erl :
>
> 11> my_time:time_spend(fun x -> 2 * X end).
> * 1: syntax error before: '->'
> 11> my_time:time_spend(Double = fun x -> 2 * X end).
> * 1: syntax error before: '->'
>
> how can I make this work ?
>
> Roelof
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> https://www.avast.com/antivirus
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roelof Wobben-2
In reply to this post by Garry Hodgson-2
Hello,

According to the Programming Erlang book second edition this is the
proper syntax.

Double = fun X -> X * 2 end.

Roelof


Op 12-8-2015 om 17:38 schreef Garry Hodgson:

> read the documentation on the proper syntax for defining funs.
>
> On 8/12/15 11:34 AM, Roelof Wobben 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. 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.
>>
>>
>> but then when I do this in erl :
>>
>> 11> my_time:time_spend(fun x -> 2 * X end).
>>  * 1: syntax error before: '->'
>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>> * 1: syntax error before: '->'
>>
>> how can I make this work ?
>>
>> Roelof
>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>> antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roberto Ostinelli
You're just missing proper parenthesis.

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

BTW there isn't such thing as time:now().



On Wed, Aug 12, 2015 at 5:42 PM, Roelof Wobben <[hidden email]> wrote:
Hello,

According to the Programming Erlang book second edition this is the proper syntax.

Double = fun X -> X * 2 end.

Roelof


Op 12-8-2015 om 17:38 schreef Garry Hodgson:

read the documentation on the proper syntax for defining funs.

On 8/12/15 11:34 AM, Roelof Wobben 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. 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.


but then when I do this in erl :

11> my_time:time_spend(fun x -> 2 * X end).
 * 1: syntax error before: '->'
11> my_time:time_spend(Double = fun x -> 2 * X end).
* 1: syntax error before: '->'

how can I make this work ?

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Siraaj Khandkar-3
In reply to this post by Roelof Wobben-2
The problem is exactly what the error message says - it is a syntax error. The message says that the syntax error is before the symbol '->'.

1) Can you find all the places where you used that symbol in the above example?
2) Now compare each one of your uses of '->' to examples in the tutorial you're reading.
3) Are you using it _exactly_ the same as in the tutorial?
4) What is different?

Hint: I see 2 places to check.


On Wed, Aug 12, 2015 at 11:34 AM, Roelof Wobben <[hidden email]> 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. 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.


but then when I do this in erl :

11> my_time:time_spend(fun x -> 2 * X end).
 * 1: syntax error before: '->'
11> my_time:time_spend(Double = fun x -> 2 * X end).
* 1: syntax error before: '->'

how can I make this work ?

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roelof Wobben-2
In reply to this post by dmkolesnikov
Correct but my question is how I can access the function with a fun.

Roelof


Op 12-8-2015 om 17:39 schreef Dmitry Kolesnikov:

> Hello,
>
> You can use built-in function
> http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1
>
> You can also check from OTP source the implementation of this function and compare it with your's
> https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160
>
> It gives you hints :-)
>
> Best Regards,
> Dmitry
>
>> On 12 Aug 2015, at 18:34, Roelof Wobben <[hidden email]> 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. 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.
>>
>>
>> but then when I do this in erl :
>>
>> 11> my_time:time_spend(fun x -> 2 * X end).
>> * 1: syntax error before: '->'
>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>> * 1: syntax error before: '->'
>>
>> how can I make this work ?
>>
>> Roelof
>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roelof Wobben-2
I think I have a too old version because I see this :

exception error: undefined function erlang:monotonic_time/0 in function
my_time:time_spend/1 (my_time.erl, line 6)

I have R16B03,

Roelof



Op 12-8-2015 om 17:47 schreef Roelof Wobben:

> Correct but my question is how I can access the function with a fun.
>
> Roelof
>
>
> Op 12-8-2015 om 17:39 schreef Dmitry Kolesnikov:
>> Hello,
>>
>> You can use built-in function
>> http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1
>>
>> You can also check from OTP source the implementation of this
>> function and compare it with your's
>> https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160
>>
>> It gives you hints :-)
>>
>> Best Regards,
>> Dmitry
>>
>>> On 12 Aug 2015, at 18:34, Roelof Wobben <[hidden email]> 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. 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.
>>>
>>>
>>> but then when I do this in erl :
>>>
>>> 11> my_time:time_spend(fun x -> 2 * X end).
>>> * 1: syntax error before: '->'
>>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>>> * 1: syntax error before: '->'
>>>
>>> how can I make this work ?
>>>
>>> Roelof
>>>
>>>
>>> ---
>>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>>> antivirussoftware.
>>> https://www.avast.com/antivirus
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> [hidden email]
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast
> antivirussoftware.
> https://www.avast.com/antivirus
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Rick Pettit
Not sure this is *exactly* what you are after, but…

-module(my_time).
-export([my_time_func/2]).

my_time_func(F, Arg) ->
  T0 = erlang:now(),
  io:format("Fun returned: ~p~n", [F(Arg)]),
  T1 = erlang:now(),
  Elapsed = timer:now_diff(T1, T0),
  io:format("Elapsed time: ~p microseconds~n", [Elapsed]).


===

rpettit-ltm:~ rpettit$ erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.4  (abort with ^G)
1> c(my_time).
{ok,my_time}
2> my_time:my_time_func(fun(X) -> 2 * X end, 3).
Fun returned: 6
Elapsed time: 73 microseconds
ok

===

Note that erlang:now/0 is deprecated, the above was just to illustrate one way to work with that fun you are having trouble with.

Again, the above may not *exactly* solve your problem, but the code works and you can feel free to play with it until you are comfortable with what is going on there.

My recommendation would be to use an iterative approach—take something which works, make very small changes and continually test. This way when you break something, you immediately know where to look.

When starting from scratch, build something *small*, make it work, then slowly add to it all the while going back to run tests, verify you didn’t introduce any bugs, etc.

-Rick

> On Aug 12, 2015, at 10:51 AM, Roelof Wobben <[hidden email]> wrote:
>
> I think I have a too old version because I see this :
>
> exception error: undefined function erlang:monotonic_time/0 in function my_time:time_spend/1 (my_time.erl, line 6)
>
> I have R16B03,
>
> Roelof
>
>
>
> Op 12-8-2015 om 17:47 schreef Roelof Wobben:
>> Correct but my question is how I can access the function with a fun.
>>
>> Roelof
>>
>>
>> Op 12-8-2015 om 17:39 schreef Dmitry Kolesnikov:
>>> Hello,
>>>
>>> You can use built-in function
>>> http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1
>>>
>>> You can also check from OTP source the implementation of this function and compare it with your's
>>> https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160
>>>
>>> It gives you hints :-)
>>>
>>> Best Regards,
>>> Dmitry
>>>
>>>> On 12 Aug 2015, at 18:34, Roelof Wobben <[hidden email]> 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. 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.
>>>>
>>>>
>>>> but then when I do this in erl :
>>>>
>>>> 11> my_time:time_spend(fun x -> 2 * X end).
>>>> * 1: syntax error before: '->'
>>>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>>>> * 1: syntax error before: '->'
>>>>
>>>> how can I make this work ?
>>>>
>>>> Roelof
>>>>
>>>>
>>>> ---
>>>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>>>> https://www.avast.com/antivirus
>>>>
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> [hidden email]
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> https://www.avast.com/antivirus
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roelof Wobben-2
Thanks, Now I see what went wrong and how to execute the fun with a
argument.

Roelof


Op 12-8-2015 om 18:04 schreef Rick Pettit:

> Not sure this is *exactly* what you are after, but…
>
> -module(my_time).
> -export([my_time_func/2]).
>
> my_time_func(F, Arg) ->
>    T0 = erlang:now(),
>    io:format("Fun returned: ~p~n", [F(Arg)]),
>    T1 = erlang:now(),
>    Elapsed = timer:now_diff(T1, T0),
>    io:format("Elapsed time: ~p microseconds~n", [Elapsed]).
>
>
> ===
>
> rpettit-ltm:~ rpettit$ erl
> Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
>
> Eshell V6.4  (abort with ^G)
> 1> c(my_time).
> {ok,my_time}
> 2> my_time:my_time_func(fun(X) -> 2 * X end, 3).
> Fun returned: 6
> Elapsed time: 73 microseconds
> ok
>
> ===
>
> Note that erlang:now/0 is deprecated, the above was just to illustrate one way to work with that fun you are having trouble with.
>
> Again, the above may not *exactly* solve your problem, but the code works and you can feel free to play with it until you are comfortable with what is going on there.
>
> My recommendation would be to use an iterative approach—take something which works, make very small changes and continually test. This way when you break something, you immediately know where to look.
>
> When starting from scratch, build something *small*, make it work, then slowly add to it all the while going back to run tests, verify you didn’t introduce any bugs, etc.
>
> -Rick
>
>> On Aug 12, 2015, at 10:51 AM, Roelof Wobben <[hidden email]> wrote:
>>
>> I think I have a too old version because I see this :
>>
>> exception error: undefined function erlang:monotonic_time/0 in function my_time:time_spend/1 (my_time.erl, line 6)
>>
>> I have R16B03,
>>
>> Roelof
>>
>>
>>
>> Op 12-8-2015 om 17:47 schreef Roelof Wobben:
>>> Correct but my question is how I can access the function with a fun.
>>>
>>> Roelof
>>>
>>>
>>> Op 12-8-2015 om 17:39 schreef Dmitry Kolesnikov:
>>>> Hello,
>>>>
>>>> You can use built-in function
>>>> http://erldocs.com/17.0/stdlib/timer.html?i=0&search=timer:tc#tc/1
>>>>
>>>> You can also check from OTP source the implementation of this function and compare it with your's
>>>> https://github.com/erlang/otp/blob/maint/lib/stdlib/src/timer.erl#L160
>>>>
>>>> It gives you hints :-)
>>>>
>>>> Best Regards,
>>>> Dmitry
>>>>
>>>>> On 12 Aug 2015, at 18:34, Roelof Wobben <[hidden email]> 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. 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.
>>>>>
>>>>>
>>>>> but then when I do this in erl :
>>>>>
>>>>> 11> my_time:time_spend(fun x -> 2 * X end).
>>>>> * 1: syntax error before: '->'
>>>>> 11> my_time:time_spend(Double = fun x -> 2 * X end).
>>>>> * 1: syntax error before: '->'
>>>>>
>>>>> how can I make this work ?
>>>>>
>>>>> Roelof
>>>>>
>>>>>
>>>>> ---
>>>>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>>>>> https://www.avast.com/antivirus
>>>>>
>>>>> _______________________________________________
>>>>> erlang-questions mailing list
>>>>> [hidden email]
>>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>> ---
>>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>>> https://www.avast.com/antivirus
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> [hidden email]
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Fred Hebert-2
In reply to this post by Roelof Wobben-2
On 08/12, Roelof Wobben wrote:
>Hello,
>
>Im trying this exercise from the programming erlang book.
>
>Roelof
>

Hi Roelof.

Honest advice here is that you really, really need to sit down and read
more carefully through the documentation you have at hand, and to try
experimenting with your programs a bit.  We've been through this months
ago already.

Here's a quick list:

Feb 2015:

- http://erlang.org/pipermail/erlang-questions/2015-February/083087.html
 Valid question from the exercise book, because too simple of a solution
 was indeed too simple.
- http://erlang.org/pipermail/erlang-questions/2015-February/083162.html
 correct implementations, but you confused strings and atoms. Those were
 exercises from 'Erlang Programming' book. Atoms are introduced on p.19,
 the exercises on p.44.
- http://erlang.org/pipermail/erlang-questions/2015-February/083197.html
 This is a function again from Erlang Programming. The precise
 implementation you are looking for for sum_acc/3 is on page 68, and is
 not actually an exercise as mentioned
- http://erlang.org/pipermail/erlang-questions/2015-February/083217.html
 You found a compile error for mismatching heads. I'm not sure when in
 the book it is, but I'd like to show you the link
 http://learnyousomeerlang.com/errors-and-exceptions#a-compilation-of-errors 
 where I compiled the common compile errors, with their explanation and
 how to fix them.
- http://erlang.org/pipermail/erlang-questions/2015-February/083233.html
 Valid enough question about list building, I have little to say here
- http://erlang.org/pipermail/erlang-questions/2015-February/083240.html
 Error on the syntax of atoms, again introduced on p.19. The error *is*
 a bit cryptic though
- http://erlang.org/pipermail/erlang-questions/2015-February/083301.html
 Exercise from the Erlang programming book. Trying the guards you had
 set in the shell with numbers would have revealed the problem directly
 (as pointed out in the first response)

Fast forward to this month:

- http://erlang.org/pipermail/erlang-questions/2015-August/085382.html
 I'll point you for some like this to the same learnyousomeerlang link
 in the future, it's also there!
- http://erlang.org/pipermail/erlang-questions/2015-August/085410.html
 The problem there was an unexported function. The error you saw was
 probably something like 'undef'. In this case, and for other errors
 happening at runtime, I'd like to redirect you to
 http://learnyousomeerlang.com/errors-and-exceptions#run-time-errors 
 which includes descriptions for such errors and ways to fix them in
 general. Note that the error is also described on page 70 of Erlang
 Programming.
- http://erlang.org/pipermail/erlang-questions/2015-August/085419.html
 Dialyzer errors are legitimately confusing for a newcomer!
- http://erlang.org/pipermail/erlang-questions/2015-August/085438.html
 Valid question from 'make it work -> make it beautiful' as a progress
- http://erlang.org/pipermail/erlang-questions/2015-August/085496.html
 This very thread. The content there is from Programming Erlang
 (Armstrong). If it's the second edition, I don't have it, but in the
 first edition, the syntax to functions is explained on page 42. In
 Erlang programming (which you also have), it's on page 190, and in
 Etudes for Erlang, which you have also looked at, they're explained in
 chapter 7
 (http://chimera.labs.oreilly.com/books/1234000000726/ch07.html)

Don't get me wrong, I appreciate people posting to the mailing list. The
thing is, I feel that it would be helpful for *your* learning as a whole
to make use of the resources you have rather than coming to the list as
often as you do. For one, the feedback loop and your progress will be
much faster!

Out of the 12 email threads I have linked here, at least 6 of them could
have been solved by re-reading the learning material you have in your
hands (because that's where you take exercises and examples from), or by
experimenting rapidly with the shell.

The other half were good questions to ask, so by all means, don't stop
asking questions. Just make sure that you're not using us as your own
private debugger!

Old timers from the industry will tell you stories of when they had to
take punched cards or hand-written programs, had to go to their
university department to make them run, wait hours or days before
finding if things were alright, and then repeating this over again for
every bug.

When you ask us to solve such problems for you while you have all the
information required, you might just be throwing yourself back 30-40
years in the past in terms of feedback loops!

You've got the material, the tools, and visibly the drive to do that
stuff. It's likely going to be simpler in the long run to make a few
experiments, run them, and see if you can figure it out (or go back and
re-read significant chapters in one of the many books you have on the
topic) than the time it takes for you to write an email and wait for a
response.

Regards,
Fred.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Joe Armstrong-2
On Wed, Aug 12, 2015 at 7:20 PM, Fred Hebert <[hidden email]> wrote:

> On 08/12, Roelof Wobben wrote:
>>
>> Hello,
>>
>> Im trying this exercise from the programming erlang book.
>>
>> Roelof
>>
>
> Hi Roelof.
>
> Honest advice here is that you really, really need to sit down and read more
> carefully through the documentation you have at hand, and to try
> experimenting with your programs a bit.  We've been through this months ago
> already.
>
> Here's a quick list:
>
> Feb 2015:
>
> - http://erlang.org/pipermail/erlang-questions/2015-February/083087.html
> Valid question from the exercise book, because too simple of a solution was
> indeed too simple.
> - http://erlang.org/pipermail/erlang-questions/2015-February/083162.html
> correct implementations, but you confused strings and atoms. Those were
> exercises from 'Erlang Programming' book. Atoms are introduced on p.19, the
> exercises on p.44. -
> http://erlang.org/pipermail/erlang-questions/2015-February/083197.html
> This is a function again from Erlang Programming. The precise implementation
> you are looking for for sum_acc/3 is on page 68, and is not actually an
> exercise as mentioned
> - http://erlang.org/pipermail/erlang-questions/2015-February/083217.html
> You found a compile error for mismatching heads. I'm not sure when in the
> book it is, but I'd like to show you the link
> http://learnyousomeerlang.com/errors-and-exceptions#a-compilation-of-errors
> where I compiled the common compile errors, with their explanation and how
> to fix them.
> - http://erlang.org/pipermail/erlang-questions/2015-February/083233.html
> Valid enough question about list building, I have little to say here
> - http://erlang.org/pipermail/erlang-questions/2015-February/083240.html
> Error on the syntax of atoms, again introduced on p.19. The error *is* a bit
> cryptic though
> - http://erlang.org/pipermail/erlang-questions/2015-February/083301.html
> Exercise from the Erlang programming book. Trying the guards you had set in
> the shell with numbers would have revealed the problem directly (as pointed
> out in the first response)
>
> Fast forward to this month:
>
> - http://erlang.org/pipermail/erlang-questions/2015-August/085382.html
> I'll point you for some like this to the same learnyousomeerlang link in the
> future, it's also there!
> - http://erlang.org/pipermail/erlang-questions/2015-August/085410.html
> The problem there was an unexported function. The error you saw was probably
> something like 'undef'. In this case, and for other errors happening at
> runtime, I'd like to redirect you to
> http://learnyousomeerlang.com/errors-and-exceptions#run-time-errors which
> includes descriptions for such errors and ways to fix them in general. Note
> that the error is also described on page 70 of Erlang Programming.
> - http://erlang.org/pipermail/erlang-questions/2015-August/085419.html
> Dialyzer errors are legitimately confusing for a newcomer!
> - http://erlang.org/pipermail/erlang-questions/2015-August/085438.html
> Valid question from 'make it work -> make it beautiful' as a progress
> - http://erlang.org/pipermail/erlang-questions/2015-August/085496.html
> This very thread. The content there is from Programming Erlang (Armstrong).
> If it's the second edition, I don't have it, but in the first edition, the
> syntax to functions is explained on page 42. In Erlang programming (which
> you also have), it's on page 190, and in Etudes for Erlang, which you have
> also looked at, they're explained in chapter 7
> (http://chimera.labs.oreilly.com/books/1234000000726/ch07.html)
>
> Don't get me wrong, I appreciate people posting to the mailing list. The
> thing is, I feel that it would be helpful for *your* learning as a whole to
> make use of the resources you have rather than coming to the list as often
> as you do. For one, the feedback loop and your progress will be much faster!
>
> Out of the 12 email threads I have linked here, at least 6 of them could
> have been solved by re-reading the learning material you have in your hands
> (because that's where you take exercises and examples from), or by
> experimenting rapidly with the shell.
>
> The other half were good questions to ask, so by all means, don't stop
> asking questions. Just make sure that you're not using us as your own
> private debugger!
>
> Old timers from the industry will tell you stories of when they had to take
> punched cards or hand-written programs, had to go to their university
> department to make them run, wait hours or days before finding if things
> were alright, and then repeating this over again for every bug.

Those were the days - it was actually three weeks.

    Week 1)
        send handwritten coding sheets to be turned into punched cards
    Week 2)
        get punched cards back and proof read them, if ok send to
        computer centre. If not ok go to beginning
     Week 3)
        Get results back from computer. The good old FORTRAN compiler
        stopped at the first syntax error it found and went no further.

       So K syntax errors took 3*K weeks to debug *before* the program ran for
        the first time.

      The van came one a week and drove the punched cards to the computer
center.

       Once we had a visit to the computer center - the programmers wore
white lab coats - I was *very* impressed.

      Years later the turn round time was down to a mere 3-hours and
we could punch our own cards.

      Now the turn-round time is the time it takes the spring under the return
button to uncoil - (apart from when running XCode that is)

     All I can say is that we thought a lot lot longer and harder before
submitting our programs - three week turn-round time makes you stare
really really hard at your code before sending it off.

   So in the old days we did a lot of thinking and the machines did
very little. Now
the machines are so fast we don't do much thinking, and in a few years time
we probably won't do any!

   and then I remember ....

    <we know> Yawn ......

    Sorry .... just an old timer

    PS. Don't set Mike off - he'll go on and on about paper-tapes ....
he thought
starting with punched cards was incredibly luxurious

/Joe

>
> When you ask us to solve such problems for you while you have all the
> information required, you might just be throwing yourself back 30-40 years
> in the past in terms of feedback loops!
>
> You've got the material, the tools, and visibly the drive to do that stuff.
> It's likely going to be simpler in the long run to make a few experiments,
> run them, and see if you can figure it out (or go back and re-read
> significant chapters in one of the many books you have on the topic) than
> the time it takes for you to write an email and wait for a response.
>
> Regards,
> Fred.
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Roelof Wobben-2
In reply to this post by Fred Hebert-2
Op 12-8-2015 om 19:20 schreef Fred Hebert:

> On 08/12, Roelof Wobben wrote:
>> Hello,
>>
>> Im trying this exercise from the programming erlang book.
>>
>> Roelof
>>
>
> Hi Roelof.
>
> Honest advice here is that you really, really need to sit down and
> read more carefully through the documentation you have at hand, and to
> try experimenting with your programs a bit.  We've been through this
> months ago already.
>
> Here's a quick list:
>
> Feb 2015:
>
> - http://erlang.org/pipermail/erlang-questions/2015-February/083087.html
> Valid question from the exercise book, because too simple of a
> solution was indeed too simple.
> - http://erlang.org/pipermail/erlang-questions/2015-February/083162.html
> correct implementations, but you confused strings and atoms. Those
> were exercises from 'Erlang Programming' book. Atoms are introduced on
> p.19, the exercises on p.44. -
> http://erlang.org/pipermail/erlang-questions/2015-February/083197.html
> This is a function again from Erlang Programming. The precise
> implementation you are looking for for sum_acc/3 is on page 68, and is
> not actually an exercise as mentioned
> - http://erlang.org/pipermail/erlang-questions/2015-February/083217.html
> You found a compile error for mismatching heads. I'm not sure when in
> the book it is, but I'd like to show you the link
> http://learnyousomeerlang.com/errors-and-exceptions#a-compilation-of-errors 
> where I compiled the common compile errors, with their explanation and
> how to fix them.
> - http://erlang.org/pipermail/erlang-questions/2015-February/083233.html
> Valid enough question about list building, I have little to say here
> - http://erlang.org/pipermail/erlang-questions/2015-February/083240.html
> Error on the syntax of atoms, again introduced on p.19. The error *is*
> a bit cryptic though
> - http://erlang.org/pipermail/erlang-questions/2015-February/083301.html
> Exercise from the Erlang programming book. Trying the guards you had
> set in the shell with numbers would have revealed the problem directly
> (as pointed out in the first response)
>
> Fast forward to this month:
>
> - http://erlang.org/pipermail/erlang-questions/2015-August/085382.html
> I'll point you for some like this to the same learnyousomeerlang link
> in the future, it's also there!
> - http://erlang.org/pipermail/erlang-questions/2015-August/085410.html
> The problem there was an unexported function. The error you saw was
> probably something like 'undef'. In this case, and for other errors
> happening at runtime, I'd like to redirect you to
> http://learnyousomeerlang.com/errors-and-exceptions#run-time-errors 
> which includes descriptions for such errors and ways to fix them in
> general. Note that the error is also described on page 70 of Erlang
> Programming.
> - http://erlang.org/pipermail/erlang-questions/2015-August/085419.html
> Dialyzer errors are legitimately confusing for a newcomer!
> - http://erlang.org/pipermail/erlang-questions/2015-August/085438.html
> Valid question from 'make it work -> make it beautiful' as a progress
> - http://erlang.org/pipermail/erlang-questions/2015-August/085496.html
> This very thread. The content there is from Programming Erlang
> (Armstrong). If it's the second edition, I don't have it, but in the
> first edition, the syntax to functions is explained on page 42. In
> Erlang programming (which you also have), it's on page 190, and in
> Etudes for Erlang, which you have also looked at, they're explained in
> chapter 7 (http://chimera.labs.oreilly.com/books/1234000000726/ch07.html)
>
> Don't get me wrong, I appreciate people posting to the mailing list.
> The thing is, I feel that it would be helpful for *your* learning as a
> whole to make use of the resources you have rather than coming to the
> list as often as you do. For one, the feedback loop and your progress
> will be much faster!
>
> Out of the 12 email threads I have linked here, at least 6 of them
> could have been solved by re-reading the learning material you have in
> your hands (because that's where you take exercises and examples
> from), or by experimenting rapidly with the shell.
>
> The other half were good questions to ask, so by all means, don't stop
> asking questions. Just make sure that you're not using us as your own
> private debugger!
>
> Old timers from the industry will tell you stories of when they had to
> take punched cards or hand-written programs, had to go to their
> university department to make them run, wait hours or days before
> finding if things were alright, and then repeating this over again for
> every bug.
>
> When you ask us to solve such problems for you while you have all the
> information required, you might just be throwing yourself back 30-40
> years in the past in terms of feedback loops!
>
> You've got the material, the tools, and visibly the drive to do that
> stuff. It's likely going to be simpler in the long run to make a few
> experiments, run them, and see if you can figure it out (or go back
> and re-read significant chapters in one of the many books you have on
> the topic) than the time it takes for you to write an email and wait
> for a response.
>
> Regards,
> Fred.
>


oke, point taken.

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Richard A. O'Keefe-2
In reply to this post by Roelof Wobben-2

On 13/08/2015, at 3:34 am, Roelof Wobben <[hidden email]> 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.)

>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to make this work

Richard A. O'Keefe-2
In reply to this post by Joe Armstrong-2

On 13/08/2015, at 5:48 am, Joe Armstrong <[hidden email]> wrote:
>    PS. Don't set Mike off - he'll go on and on about paper-tapes ....
> he thought
> starting with punched cards was incredibly luxurious

Not if you had to use a hand punch it wasn't!


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions