Urban Hafner : Tag trigger, everything about trigger http://bettong.net/tag/trigger.rss en-us 40 CTM: Exercise 4-7 <h3>The problem</h3> <p><em>Programmed triggers using higher-order programming.</em> Programmed triggers can be implemented by using higher-order programming instead of concurrency and dataflow variables. The producer passes a zero-argument function <code>F</code> to the consumer. Whenever the consumer needs an element, it calls the function. This returns a pair <code>X#F2</code> where <code>X</code> is the next stream element and <code>F2</code> is a function that has the same behavior as <code>F</code>. Modify the example of section 4.3.3 to use this technique.</p> <h3>My solution</h3> <div class="typocode"><pre><code class="typocode_default ">declare HGenerate HSum fun {HGenerate N} N#fun {$} {HGenerate N+1} end end fun {HSum Limit F A} if Limit&gt;0 then X#F2={F} in {HSum Limit-1 F2 A+X} else A end end local X F in X#F={HGenerate ~1} % Yes, the ~1 is correct! {Browse {HSum 150000 F 0}} end</code></pre></div> Sun, 06 Nov 2005 16:40:00 +0000 urn:uuid:13462bd4-8a5e-4259-aa9b-6540a0c81ea3 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/06/ctm-exercise-4-7#comments CTM book exercise mozart oz programmed trigger higher-order programming http://bettong.net/2005/11/06/ctm-exercise-4-7