There's the lazy library, but it's not functional, i.e. I define a to be 1, define b as a + a, force b, it gives 2, but then I force set a to be 5, and it gives b as 2 again.
>>2
That doesn't matter, the problem is when it returns b it overwrites b with the result of the forced promise. Wth would they do this if it's supposed to be based on scheme.
>>1,3
After being forced the first time, the promise will cache its result. Further forceing will return that result.
> racket
Welcome to Racket v5.1.> (define a 2)> (define b (delay (+ a 2)))> (set! a 3)> (force b)
5> (set! a 4)> (force b)
5
> racket -il lazy
Welcome to Racket v5.1.> (define a 2)> (define b (+ a 2))> (force (set! a 3))> (force b)
5> (force (set! a 4))> (force b)
5
> scheme
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2010 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Tuesday March 9, 2010 at 6:41:34 PM
Release 9.0.1 || Microcode 15.1 || Runtime 15.7 || SF 4.41 || LIAR/i386 4.118 || Edwin 3.116
1 ]=> (define a 2)
;Value: a
1 ]=> (define b (delay (+ a 2)))
;Value: b
1 ]=> (set! a 3)
;Value: 2
1 ]=> (force b)
;Value: 5
1 ]=> (set! a 4)
;Value: 3
1 ]=> (force b)
;Value: 5
>>8 Heh, I was thinking "Scheme" as in "the way the interpreter (and theory) worked in the SICP videos"
Yes, it's different, but that's because you'd need to re-evaluate everything.
What if the promise had some side effects? They would be evaluated twice and that's not an acceptable behaviour.
>>10 (define a 2)
(define-alias b (+ a 2))
(+ a b) ; 6
(set! a 3)
(+ a b) ; 8
It's just an example, though, don't do it.
As a function, you'd need to: (define a 2)
(define (b) a)
(+ a (b))
Name:
Anonymous2011-02-19 17:48
>>12
One of the big things in Scheme was the whole "you should be able to do thing without the language doing shit in the background"
Isn't it bad programming to assume the programmer is ignorant of side effects? Is there even ANY possible way in Racket to have the expression (+ a b) hold valid, where a is a value and b is an expression?
>>14 Is there even ANY possible way >>11,13
With that macro, ``b'' gets replaced with a literal (+ a 2).
A function {i is} a value, (+ a b) is just (+ #<variable> #<procedure>). Other than that, no, variables can hold only values.