# [erlang-questions] Name For This Pattern?

David Mercer <>
Tue Aug 21 00:46:43 CEST 2007

```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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070820/b1fe9119/attachment.html>
```