A basic question

Richard O'Keefe raoknz@REDACTED
Thu Mar 5 14:48:11 CET 2020


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 <gksalil@REDACTED> 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 <raoknz@REDACTED> 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 <gksalil@REDACTED> 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


More information about the erlang-questions mailing list