home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / scheme / 2151 < prev    next >
Encoding:
Internet Message Format  |  1992-09-07  |  3.4 KB

  1. Path: sparky!uunet!mcsun!uknet!reading!shrchin
  2. From: shrchin@csug.cs.reading.ac.uk (Jonathan H. N. Chin)
  3. Newsgroups: comp.lang.scheme
  4. Subject: Re: trampolines
  5. Message-ID: <shrchin.715668205@reading>
  6. Date: 5 Sep 92 04:43:25 GMT
  7. References: <shrchin.715636752@reading> <Iedzvq_00VIBMC7kwP@andrew.cmu.edu>
  8. Sender: news@csug.cs.reading.ac.uk
  9. Organization: University of Reading
  10. Lines: 86
  11. Nntp-Posting-Host: rosemary
  12.  
  13. sm86+@andrew.cmu.edu (Stefan Monnier) writes:
  14. >Excerpts from netnews.comp.lang.scheme: 4-Sep-92 trampolines Jonathan H.
  15. >N. Chin@csug (2303)
  16. >> Ignoring promises that refer to their own values, it seems that the
  17. >> above code would after the first "force" always have to perform a
  18. >> redundant `if' test of `result-ready?'.  I should have thought that
  19. >> this requirement could be removed, and the code I have tried seems to
  20. >> do this:
  21. >>
  22. >> (define make-trampoline
  23. >>   (lambda (proc)
  24. >>     (letrec ((the-promise (lambda()
  25. >>                             (let ((result (proc)))
  26. >>                               (begin
  27. >>                                 (set! the-promise (lambda()
  28. >>                                                     result))
  29. >>                                 result)))))
  30. >>       the-promise)))
  31. >>
  32. >> where `make-trampoline' is used in place of `make-promise'.
  33. >> (aside: what would be a reasonable way to reformat the above?)
  34.  
  35. > [...makes the legitimate point that my code is wrong...]
  36. >Bad Luck !
  37.  
  38. Rats. Thought I had tested that code. Obviously not. Okay then. How about
  39. we add another level thus:
  40.  
  41. (define make-trampoline[2]
  42.   (lambda (proc)
  43.     (letrec ((dummy (lambda()
  44.                       (let ((result (proc)))
  45.                         (begin
  46.                           (set! dummy (lambda()
  47.                                         result))
  48.                            result)))))
  49.       (lambda()
  50.         (dummy)))))
  51.  
  52. I tried this code, and it worked (or at least it seemed to... 8^).
  53.  
  54. However, I now have an extra layer of indirection -- the extra function call.
  55. So, have I just lost the advantage of removing the test in the first place?
  56.  
  57. I tend to think in C terms. Would I be correct to think that in code such as:
  58.  
  59. (define x 0)
  60. (define trampoline (make-trampoline (lambda()
  61.                                       (+ x 1)))) 
  62.  
  63. that `trampoline' "points to" the closure created by the `letrec', rather
  64. than pointing to `the-promise' which points to the closure. And hence when
  65. `the-promise' changes, there is no affect on `trampoline'. Something like:
  66.  
  67.                      trampoline
  68.  
  69.                          |
  70.                          v
  71.  
  72.    the-promise  -->  #<closure>
  73.  
  74. instead of:
  75.  
  76.    trampoline --> the-promise --> #<closure>
  77.  
  78. The question I then have is why does changing `dummy' in `make-trampoline[2]'
  79. affect the closure "(lambda() (dummy))" although changes to `the-promise'
  80. in the definition of `trampoline' (substituting `make-trampoline' for
  81. `make-trampoline[2]') are *not* propagated?
  82.  
  83.  
  84. From my previous post:
  85. 1) What prevents the R^4 `make-promise' from recursing indefinitely?
  86.  
  87. and now
  88. 2) Does `make-trampoline[2]' save over `make-promise' ?
  89. 3) If (2), how do I extend `make-trampoline[2]' to allow promises that
  90.    refer to their values?
  91.  
  92. Thanks again,
  93. Jonathan
  94. -- 
  95. Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
  96. shrchin@uk.ac.rdg.susssys1 \ University of Reading \
  97. bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
  98. jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \
  99.