tag:bettong.net,:/tag/programming Urban Hafner : Tag programming, everything about programming 2007-05-08T08:01:12+00:00 Typo tag:bettong.net,:Article/66 2005-11-06T16:40:00+00:00 2007-05-08T08:01:12+00:00 Urban Hafner CTM: Exercise 4-7

The problem

Programmed triggers using higher-order programming. Programmed triggers can be implemented by using higher-order programming instead of concurrency and dataflow variables. The producer passes a zero-argument function F to the consumer. Whenever the consumer needs an element, it calls the function. This returns a pair X#F2 where X is the next stream element and F2 is a function that has the same behavior as F. Modify the example of section 4.3.3 to use this technique.

My solution

declare HGenerate HSum
fun {HGenerate N}
   N#fun {$} {HGenerate N+1} end
end

fun {HSum Limit F A}
   if Limit>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

The problem

Programmed triggers using higher-order programming. Programmed triggers can be implemented by using higher-order programming instead of concurrency and dataflow variables. The producer passes a zero-argument function F to the consumer. Whenever the consumer needs an element, it calls the function. This returns a pair X#F2 where X is the next stream element and F2 is a function that has the same behavior as F. Modify the example of section 4.3.3 to use this technique.

My solution

declare HGenerate HSum
fun {HGenerate N}
   N#fun {$} {HGenerate N+1} end
end

fun {HSum Limit F A}
   if Limit>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