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

  1. Path: sparky!uunet!pipex!unipalm!uknet!reading!shrchin
  2. From: shrchin@csug.cs.reading.ac.uk (Jonathan H. N. Chin)
  3. Newsgroups: comp.lang.scheme
  4. Subject: trampolines
  5. Message-ID: <shrchin.715636752@reading>
  6. Date: 4 Sep 92 19:59:12 GMT
  7. Sender: news@csug.cs.reading.ac.uk
  8. Organization: University of Reading
  9. Lines: 58
  10. Nntp-Posting-Host: rosemary
  11.  
  12. I'm teaching myself scheme and am currently looking at `delay' and
  13. `force'. The sample definition of `make-promise' in R^4 is:
  14.  
  15. (define make-promise
  16.   (lambda (proc)
  17.     (let ((result-ready? #f)
  18.           (result #f))
  19.       (lambda ()
  20.         (if result-ready?
  21.             result
  22.             (let ((x (proc)))
  23.               (if result-ready?
  24.                   result
  25.                   (begin (set! result-ready? #t)
  26.                          (set result x)
  27.                          result))))))))
  28.  
  29. I believe the rationale for the second `if' to be that the promise
  30. may refer to its own value. My first question then is what prevents
  31. an infinite stack of calls of `proc' from forming, where `proc' is, I
  32. believe, the promise? Is it just a case of making sure that the
  33. promise itself terminates, or does `make-promise' contain some subtle
  34. device that ensures termination which I have overlooked?
  35.  
  36. Ignoring promises that refer to their own values, it seems that the
  37. above code would after the first "force" always have to perform a
  38. redundant `if' test of `result-ready?'.  I should have thought that
  39. this requirement could be removed, and the code I have tried seems to
  40. do this:
  41.  
  42. (define make-trampoline
  43.   (lambda (proc)
  44.     (letrec ((the-promise (lambda()
  45.                             (let ((result (proc)))
  46.                               (begin
  47.                                 (set! the-promise (lambda()
  48.                                                     result))
  49.                                 result)))))
  50.       the-promise)))
  51.  
  52. where `make-trampoline' is used in place of `make-promise'.
  53. (aside: what would be a reasonable way to reformat the above?)
  54.  
  55. How would I need to modify this code to encompass promises that refer
  56. to their own values?
  57.  
  58. Thanks in advance,
  59. Jonathan
  60. --
  61. Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
  62. shrchin@uk.ac.rdg.susssys1 \ University of Reading \
  63. bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
  64. jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \
  65. -- 
  66. Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
  67. shrchin@uk.ac.rdg.susssys1 \ University of Reading \
  68. bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
  69. jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \
  70.