Concepts+Techniques+and+Models+of+Computer+Programming_Part14

Concepts Techniques - 348 Declarative Concurrency fun lazy cfw_Three cfw_Delay 1000 3 end Calculating cfw_Three 0 returns 3 after a 1000

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
348 Declarative Concurrency fun lazy {Three} {Delay 1000} 3 end Calculating {Three}+0 returns 3 after a 1000 millisecond delay. This is as expected, since the addition needs the result of {Three} .N owc a l c u l a t e {Three}+0 three times in succession. Each calculation waits 1000 millisec- onds. How can this be, since Three is supposed to be lazy. Shouldn’t its result be calculated only once? 11. Laziness and concurrency I . This exercise looks closer at the concurrent behavior of lazy execution. Execute the following: fun lazy {MakeX} {Browse x} {Delay 3000} 1 end fun lazy {MakeY} {Browse y} {Delay 6000} 2 end fun lazy {MakeZ} {Browse z} {Delay 9000} 3 end X={MakeX} Y={MakeY} Z={MakeZ} {Browse (X+Y)+Z} This displays x and y immediately, z after 6 seconds, and the result 6 after 15 seconds. Explain this behavior. What happens if (X+Y)+Z is replaced by X+(Y+Z) or by thread X+Y end +Z ? Which form gives the final result the quickest? How would you program the addition of n integers i 1 , ..., i n , given that integer i j only appears after t j milliseconds, so that the final result appears the quickest? 12. Laziness and concurrency II . Let us compare the kind of incremental- ity we get from laziness and from concurrency. Section 4.3.1 gives a pro- ducer/consumer example using concurrency. Section 4.5.3 gives the same producer/consumer example using laziness. In both cases, it is possible for the output stream to appear incrementally. What is the difference? What happens if you use both concurrency and laziness in the producer/consumer example? 13. Laziness and monolithic functions . Consider the following two defini- tions of lazy list reversal: fun lazy {Reverse1 S} fun {Rev S R} case S of nil then R [] X|S2 then {Rev S2 X|R} end end in {Rev S nil} end fun lazy {Reverse2 S} fun lazy {Rev S R} Copyright c ± 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4.11 Exercises 349 case S of nil then R [] X|S2 then {Rev S2 X|R} end end in {Rev S nil} end What is the difference in behavior between {Reverse1 [a b c]} and {Reverse2 [a b c]} ? Do the two definitions calculate the same result? Do they have the same lazy behavior? Explain your answer in each case. Finally, compare the execution efficiency of the two definitions. Which definition would you use in a lazy program? 14. Laziness and iterative computation . In the declarative model, one advantage of dataflow variables is that the straightforward definition of Append is iterative. For this exercise, consider the straightforward lazy version of Append without dataflow variables, as defined in Section 4.5.7. Is it iterative? Why or why not? 15. Performance of laziness . For this exercise, take some declarative pro- grams you have written and make them lazy by declaring all routines as lazy. Use lazy versions of all built-in operations, for example addition be- comes Add , which is defined as fun lazy {Add X Y} X+Y end .Compa re the behavior of the original eager programs with the new lazy ones. What is the difference in efficiency? Some functional languages, such as Haskell and Miranda, implicitly consider all functions as lazy. To achieve reasonable
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 30

Concepts Techniques - 348 Declarative Concurrency fun lazy cfw_Three cfw_Delay 1000 3 end Calculating cfw_Three 0 returns 3 after a 1000

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online