[erlang-questions] Name For This Pattern?

Joe Armstrong <>
Tue Aug 21 15:04:25 CEST 2007


My last mail got truncated ....(don't type emacs commands in gmail :-)

On 8/21/07, Joe Armstrong <> wrote:

Another *completely different* way to represent lazy lists is as processes

You send a next message to the process and it delivers the next integer.

Create like this:

 Pid = spawn(fun() -> loop(0) end).

 where

 loop(N) ->
     receive
        {From, next} ->
            From ! {self(), N},
            loop(N+1)
     end.

 You need an access function to hide the call to the counter process

 bump(Pid) ->
     .Pid ! {self(), next},
      receive
        {Pid, Val} ->
            Val
     end.

Then you get new values each time you call bump(Pid)

Sometimes this method is better than using a fun, depends on what
you're trying to do
creating little networks of processes is equivalent to doing function
compositions

/Joe


> On 8/21/07, David Mercer <> wrote:
> >
> >
> >
> >
> > OK, so I want to generate a sequence based on some mathematical formula.
> > For instance, [1, 2, 3, 4, 5, …] or [1, 2, 4, 8, 16, …].  Since these are
> > infinite, I cannot implement them as lists.  Instead, I implement them as a
> > function:
> >
> >
> >
> > 2> % [1, 2, 3, 4, 5, ...]
> >
> > 2> F0a = FF(fun(X) -> X + 1 end, 1).
> >
> > #Fun<erl_eval.20.112921583>
> >
> > 3> {_, F1a} = F0a().
> >
> > {1,#Fun<erl_eval.20.112921583>}
> >
> > 4> {_, F2a} = F1a().
> >
> > {2,#Fun<erl_eval.20.112921583>}
> >
> > 5> {_, F3a} = F2a().
> >
> > {3,#Fun<erl_eval.20.112921583>}
> >
> > 6> {_, F4a} = F3a().
> >
> > {4,#Fun<erl_eval.20.112921583>}
> >
> > 7> {_, F5a} = F4a().
> >
> > {5,#Fun<erl_eval.20.112921583>}
> >
> > 12> % [1, 2, 4, 8, 16, ...]
> >
> > 12> F0b = FF(fun(X) -> 2 * X end, 1).
> >
> > #Fun<erl_eval.20.112921583>
> >
> > 13> {_, F1b} = F0b().
> >
> > {1,#Fun<erl_eval.20.112921583>}
> >
> > 14> {_, F2b} = F1b().
> >
> > {2,#Fun<erl_eval.20.112921583>}
> >
> > 15> {_, F3b} = F2b().
> >
> > {4,#Fun<erl_eval.20.112921583>}
> >
> > 16> {_, F4b} = F3b().
> >
> > {8,#Fun<erl_eval.20.112921583>}
> >
> > 17> {_, F5b} = F4b().
> >
> > {16,#Fun<erl_eval.20.112921583>}
> >
> >
> >
> > I am sure many of you have done this before, and I haven't discovered
> > anything new, but I wanted to figure it out for myself, so I did.  The
> > function I came up with was:
> >
> >
> >
> > FF = fun(F, X0) ->
> >
> >   G = fun(F, X0, G) ->
> >
> >     { X0, fun() -> G(F, F(X0), G) end }
> >
> >   end,
> >
> >   fun() -> G(F, X0, G) end
> >
> > end.
> >
> >
> >
> > 1.       Is this the way you'd have done this?
> >
> >
> >
> > 2.       Is there a name for this pattern?  I was calling it a continuation
> > in my mind, because the second element of the output 2-tuple is a
> > continuation function to give the next value, but I thought maybe there is a
> > more specific name for this use of continuations.
> >
> >
> >
> > Cheers,
> >
> >
> >
> > David
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>



More information about the erlang-questions mailing list