<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:Arial;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoAutoSig, li.MsoAutoSig, div.MsoAutoSig
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
span.Typed
{font-family:"Courier New";}
span.tty
{font-family:"Courier New";}
span.Name
{font-style:italic;}
span.Variable
{font-family:"Times New Roman";
font-style:italic;}
span.EmailStyle21
{font-family:Arial;
color:windowtext;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal>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:</p>
<p class=MsoNormal> </p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>2>
% <b><span style='color:red'>[1, 2, 3, 4, 5, ...]</span></b></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>2>
F0a = FF(fun(X) -> <b><span style='color:red'>X + 1</span></b> end, 1).</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>#Fun<erl_eval.20.112921583></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>3>
{_, F1a} = F0a().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>1</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>4>
{_, F2a} = F1a().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>2</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>5>
{_, F3a} = F2a().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>3</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>6>
{_, F4a} = F3a().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>4</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>7>
{_, F5a} = F4a().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>5</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>12>
% <b><span style='color:red'>[1, 2, 4, 8, 16, ...]</span></b></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>12>
F0b = FF(fun(X) -> <b><span style='color:red'>2 * X</span></b> end, 1).</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>#Fun<erl_eval.20.112921583></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>13>
{_, F1b} =
F0b().
</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>1</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>14>
{_, F2b} = F1b().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>2</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>15>
{_, F3b} = F2b().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>4</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>16>
{_, F4b} = F3b().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>8</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>17>
{_, F5b} = F4b().</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>{<b><span
style='color:red'>16</span></b>,#Fun<erl_eval.20.112921583>}</span></p>
<p class=MsoNormal> </p>
<p class=MsoNormal>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:</p>
<p class=MsoNormal> </p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>FF
= fun(F, X0) -></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>
G = fun(F, X0, G) -></span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>
{ X0, fun() -> G(F, F(X0), G) end }</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>
end,</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>
fun() -> G(F, X0, G) end</span></p>
<p class=MsoNormal style='margin-left:.5in'><span style='font-family:Consolas'>end.</span></p>
<p class=MsoNormal> </p>
<p class=MsoNormal style='margin-left:.25in;text-indent:-.25in'>1.<span
style='font:7.0pt "Times New Roman"'> </span>Is
this the way you’d have done this?</p>
<p class=MsoNormal> </p>
<p class=MsoNormal style='margin-left:.25in;text-indent:-.25in'>2.<span
style='font:7.0pt "Times New Roman"'> </span>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.</p>
<p class=MsoNormal> </p>
<p class=MsoNormal>Cheers,</p>
<p class=MsoNormal> </p>
<p class=MsoNormal>David</p>
</div>
</body>
</html>