home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!unipalm!uknet!reading!shrchin
- From: shrchin@csug.cs.reading.ac.uk (Jonathan H. N. Chin)
- Newsgroups: comp.lang.scheme
- Subject: trampolines
- Message-ID: <shrchin.715636752@reading>
- Date: 4 Sep 92 19:59:12 GMT
- Sender: news@csug.cs.reading.ac.uk
- Organization: University of Reading
- Lines: 58
- Nntp-Posting-Host: rosemary
-
- I'm teaching myself scheme and am currently looking at `delay' and
- `force'. The sample definition of `make-promise' in R^4 is:
-
- (define make-promise
- (lambda (proc)
- (let ((result-ready? #f)
- (result #f))
- (lambda ()
- (if result-ready?
- result
- (let ((x (proc)))
- (if result-ready?
- result
- (begin (set! result-ready? #t)
- (set result x)
- result))))))))
-
- I believe the rationale for the second `if' to be that the promise
- may refer to its own value. My first question then is what prevents
- an infinite stack of calls of `proc' from forming, where `proc' is, I
- believe, the promise? Is it just a case of making sure that the
- promise itself terminates, or does `make-promise' contain some subtle
- device that ensures termination which I have overlooked?
-
- Ignoring promises that refer to their own values, it seems that the
- above code would after the first "force" always have to perform a
- redundant `if' test of `result-ready?'. I should have thought that
- this requirement could be removed, and the code I have tried seems to
- do this:
-
- (define make-trampoline
- (lambda (proc)
- (letrec ((the-promise (lambda()
- (let ((result (proc)))
- (begin
- (set! the-promise (lambda()
- result))
- result)))))
- the-promise)))
-
- where `make-trampoline' is used in place of `make-promise'.
- (aside: what would be a reasonable way to reformat the above?)
-
- How would I need to modify this code to encompass promises that refer
- to their own values?
-
- Thanks in advance,
- Jonathan
- --
- Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
- shrchin@uk.ac.rdg.susssys1 \ University of Reading \
- bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
- jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \
- --
- Jonathan H N Chin (9 kyu) \ Dept. of Cybernetics, \ "Respondeo, etsi mutabor"
- shrchin@uk.ac.rdg.susssys1 \ University of Reading \
- bq305@cleveland.freenet.edu \ Box 225, Whiteknights \ < Rosenstock-Huessy >
- jockstrap,mandy@CyberSpaceII \ Reading, RG6 2AY, U K \
-