[erlang-questions] Name For This Pattern?

Joe Armstrong erlang@REDACTED
Tue Aug 21 14:56:32 CEST 2007


Another *completly different* way to represent them is as processes


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

creates a counter process

where

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

You need an access fucntion to hide the call to the counter

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



On 8/21/07, David Mercer <dmercer@REDACTED> 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
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list