Griff <gret
@gmail.com> writes:
> What is the role of promise and force?
> This weekend I read the R5RS spec. Before then I hadn't heard anything
> about promise and force, in other words, they don't get blogged about
> or written up much. Why not? Are they important? For what kinds of
> things are they used?
> It is suggested that they allow for you to write programs in a true
> functional style.
They implement lazy evaluation.
For example, if you try to implement if as a function (instead of
syntax or macro):
(define (if test then else)
(cond (test then)
(#t else)))
you get the problem that the three arguments are evaluated before the
if function is called:
(if #t (display "yes ") (display "no "))
will print:
yes no
or:
no yes
depending on the implementation, while we expected it to print only:
yes
To avoid the evaluation branches before calling if, we can embed them
in promises:
(if test (delay (display "yes ")) (delay (display "no ")))
with if implemented as:
(define (if test then else)
(cond (test (force then))
(#t (force else))))
then:
(if #t (delay (display "yes ")) (delay (display "no ")))
will print only:
yes
> Are they similar to the goal of Haskell monads in
> that they let you "do it all functionally?"
No, this is not equivalent to monads.
--
__Pascal Bourguignon__ http://www.informatimago.com/
PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
In article <87irb57c0k.@thalassa.lan.informatimago.com>,
Pascal Bourguignon <p@informatimago.com> wrote:
> To avoid the evaluation branches before calling if, we can embed them
> in promises:
> (if test (delay (display "yes ")) (delay (display "no ")))
> with if implemented as:
> (define (if test then else)
> (cond (test (force then))
> (#t (force else))))
Is that really a good example of the use of promises? Wouldn't it be
more appropriate to do this with ordinary procedures:
(define (if test then else)
(cond (test (then))
(#t (else))))
(if test (lambda () (display "yes ")) (lambda () (display "no ")))
The difference between a promise and an ordinary procedure is that a
promise can be forced multiple times, but will only execute the side
effects the first time -- it saves the result for future forces. Do you
have a simple example that demonstrates this?
--
Barry Margolin, bar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
The notion of this is certainly captured by Eiffel's 'once' keyword.
Barry Margolin wrote:
> The difference between a promise and an ordinary procedure is that a
> promise can be forced multiple times, but will only execute the side
> effects the first time -- it saves the result for future forces. Do you
> have a simple example that demonstrates this?
How about:
(let ((a (begin
(delay (display "foo"))
2)))
(+ (force a) (force a)))
I don't have an interpreter to hand, so I haven't tested it.
--
Simon Richard Clarkstone:
s.r.cl?rkst@durham.ac.uk/s?m?n.cl?rkst@hotmail.com
"August 9 - I just made my signature file. Its only 6 pages long.
I will have to work on it some more." -- _Diary of an AOL User_