[erlang-questions] Name For This Pattern?

Ludovic Coquelle lcoquelle@REDACTED
Tue Aug 21 02:45:16 CEST 2007


I think it can be called "lazy-list" because it behave like a list. However
it is often called "stream" because the internal structure may really differ
from a list (like in your example). There is a section in SICP about streams
(http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5).

For other erlang related code, there is an implementation in user
contribution of trapexit, and I wrote a very simple one too
http://khigia.wordpress.com/2007/05/07/44/ (the end contains few links which
could interest you).

Hope this help.

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070821/a4059520/attachment.htm>


More information about the erlang-questions mailing list