A basic question

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

A basic question

salil GK
Hello
   I just started learning Erlang. I fail to understand the error I am
getting while compiling the below program.

>>>>>

-module(hw).

%% API
-export([ for/2, forloop/1]).


for([],_) ->
  [];
for([X|Xs], Func) ->
  Func(X),
  for(Xs, Func).

forloop(X) ->
  Fn = myfunc(Val) ->
      io:fwrite("From myfunc ~w" , [Val]) end,
  for(X, Fn).

<<<<<

basically I am trying to make a for loop with a call back function.
I am getting the following error while compiling
hw.erl:23: syntax error before: '->'
line 23 is
  Fn = myfunc(Val) ->

~S
Reply | Threaded
Open this post in threaded view
|

Re: A basic question

Mikael Pettersson-5
On Sun, Mar 1, 2020 at 10:28 AM salil GK <[hidden email]> wrote:

> forloop(X) ->
>   Fn = myfunc(Val) ->
>       io:fwrite("From myfunc ~w" , [Val]) end,
>   for(X, Fn).
>
> <<<<<
>
> basically I am trying to make a for loop with a call back function.
> I am getting the following error while compiling
> hw.erl:23: syntax error before: '->'
> line 23 is
>   Fn = myfunc(Val) ->

The syntax is "Fn = fun(Val) -> ... end", where "fun" and "end" are
keywords, not identifiers.
See https://erlang.org/doc/reference_manual/expressions.html#fun-expressions
Reply | Threaded
Open this post in threaded view
|

Re: A basic question

Richard O'Keefe
In reply to this post by salil GK
Just as a matter of curiosity, and perhaps as a step towards
improving documentation, where did you get the idea that
myfunc(Val) ->
was valid Erlang?

Are you aware of the on-line reference manual at
https://erlang.org/doc/reference_manual/introduction.html
Section 8.18 describes 'fun's.


On Sun, 1 Mar 2020 at 22:28, salil GK <[hidden email]> wrote:

>
> Hello
>    I just started learning Erlang. I fail to understand the error I am
> getting while compiling the below program.
>
> >>>>>
>
> -module(hw).
>
> %% API
> -export([ for/2, forloop/1]).
>
>
> for([],_) ->
>   [];
> for([X|Xs], Func) ->
>   Func(X),
>   for(Xs, Func).
>
> forloop(X) ->
>   Fn = myfunc(Val) ->
>       io:fwrite("From myfunc ~w" , [Val]) end,
>   for(X, Fn).
>
> <<<<<
>
> basically I am trying to make a for loop with a call back function.
> I am getting the following error while compiling
> hw.erl:23: syntax error before: '->'
> line 23 is
>   Fn = myfunc(Val) ->
>
> ~S
Reply | Threaded
Open this post in threaded view
|

Re: A basic question

salil GK
Hi Richard

     In fact I am slowly migrating from other languages to Erlang and
didn't realise that function pointer  has a syntax.
     Yes the documentation is self explanatory.


On Mon, 2 Mar 2020 at 17:08, Richard O'Keefe <[hidden email]> wrote:

>
> Just as a matter of curiosity, and perhaps as a step towards
> improving documentation, where did you get the idea that
> myfunc(Val) ->
> was valid Erlang?
>
> Are you aware of the on-line reference manual at
> https://erlang.org/doc/reference_manual/introduction.html
> Section 8.18 describes 'fun's.
>
>
> On Sun, 1 Mar 2020 at 22:28, salil GK <[hidden email]> wrote:
> >
> > Hello
> >    I just started learning Erlang. I fail to understand the error I am
> > getting while compiling the below program.
> >
> > >>>>>
> >
> > -module(hw).
> >
> > %% API
> > -export([ for/2, forloop/1]).
> >
> >
> > for([],_) ->
> >   [];
> > for([X|Xs], Func) ->
> >   Func(X),
> >   for(Xs, Func).
> >
> > forloop(X) ->
> >   Fn = myfunc(Val) ->
> >       io:fwrite("From myfunc ~w" , [Val]) end,
> >   for(X, Fn).
> >
> > <<<<<
> >
> > basically I am trying to make a for loop with a call back function.
> > I am getting the following error while compiling
> > hw.erl:23: syntax error before: '->'
> > line 23 is
> >   Fn = myfunc(Val) ->
> >
> > ~S
Reply | Threaded
Open this post in threaded view
|

Re: A basic question

Richard O'Keefe
Er, what is a "function pointer" and why wouldn't it have a syntax?
Functions don't have to have names.
forloop(X) ->
    for(X, fun (Val) ->
        io:fwrite("From myfunc ~w~n", [Val])
    end).
should work just fine.

On Thu, 5 Mar 2020 at 16:01, salil GK <[hidden email]> wrote:

>
> Hi Richard
>
>      In fact I am slowly migrating from other languages to Erlang and
> didn't realise that function pointer  has a syntax.
>      Yes the documentation is self explanatory.
>
>
> On Mon, 2 Mar 2020 at 17:08, Richard O'Keefe <[hidden email]> wrote:
> >
> > Just as a matter of curiosity, and perhaps as a step towards
> > improving documentation, where did you get the idea that
> > myfunc(Val) ->
> > was valid Erlang?
> >
> > Are you aware of the on-line reference manual at
> > https://erlang.org/doc/reference_manual/introduction.html
> > Section 8.18 describes 'fun's.
> >
> >
> > On Sun, 1 Mar 2020 at 22:28, salil GK <[hidden email]> wrote:
> > >
> > > Hello
> > >    I just started learning Erlang. I fail to understand the error I am
> > > getting while compiling the below program.
> > >
> > > >>>>>
> > >
> > > -module(hw).
> > >
> > > %% API
> > > -export([ for/2, forloop/1]).
> > >
> > >
> > > for([],_) ->
> > >   [];
> > > for([X|Xs], Func) ->
> > >   Func(X),
> > >   for(Xs, Func).
> > >
> > > forloop(X) ->
> > >   Fn = myfunc(Val) ->
> > >       io:fwrite("From myfunc ~w" , [Val]) end,
> > >   for(X, Fn).
> > >
> > > <<<<<
> > >
> > > basically I am trying to make a for loop with a call back function.
> > > I am getting the following error while compiling
> > > hw.erl:23: syntax error before: '->'
> > > line 23 is
> > >   Fn = myfunc(Val) ->
> > >
> > > ~S