Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Lazy in racket

Name: Anonymous 2011-02-19 17:04

How2lazy in DrRacket?

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.

Any way around this?

Name: Anonymous 2011-02-19 17:10

>>1
Don't use set!, that's not functional.

Name: sage 2011-02-19 17:12

>>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.

Name: Anonymous 2011-02-19 17:17

Ugh, both racket base and lazy racket are not working right, it even overwrites a lambda expression with the result of evaluating it once.

Name: Anonymous 2011-02-19 17:23

>>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

Name: Anonymous 2011-02-19 17:24

>>5
I hate you shiichan. I really do.

>>4
see >>5, even MIT/GNU Scheme does it.

Name: Anonymous 2011-02-19 17:30

>>1
This is not a product of lazy evaluation. If you want `b` to change every time `a` does, then make `b` a function.

Name: Anonymous 2011-02-19 17:30

>>6
Heh, I was thinking "Scheme" as in "the way the interpreter (and theory) worked in the SICP videos"

So then what's the solution, if neither of these are functional?

Is there a way to turn off caching in Racket/Scheme?

Name: Anonymous 2011-02-19 17:33

>>7
Ah, I guess there's a difference between ((lambda (b) b) (+ a a)) and (define (b) (+ a a))

Name: Anonymous 2011-02-19 17:34

>>9
Ugh, and I can't just DO (+ a b), because b is function.

Why do they do this. Why don't they allow symbols to represent values that are functions.

Name: Anonymous 2011-02-19 17:37

>>7
Or better:
(define-syntax (define-alias stx)
  (syntax-case stx ()
    ((~ id w)
     (identifier? #'w)
     #'(define-syntax id (make-rename-transformer #'w)))
    ((~ id w)
     #'(define-syntax (id stx)
         (syntax-case stx ()
           (b (identifier? #'b) #'w))))))

(define a 2)
(define-alias b (+ a 2))
b ; 4
(set! a 3)
b ; 5
(set! a 4)
b ; 6
(define-alias c a)
c ; 4
(set! a 3)
c ; 3

Name: Anonymous 2011-02-19 17:41

>>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.

Name: Anonymous 2011-02-19 17:43

>>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: Anonymous 2011-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?

Name: Anonymous 2011-02-19 18:00

ITT we have no idea what ``lazy'' is.

Name: Anonymous 2011-02-19 18:03

>>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.

Name: Anonymous 2011-02-19 18:23

>>16
Ah, well thanks anyway.

Guess this limits to what I can teach SICP with :(

Name: Anonymous 2011-02-19 18:25

TOO LISP; DIDN'T READ

Name: Anonymous 2011-02-19 19:42

>>18
MY ANUS; HAX IT

Name: Anonymous 2012-12-08 15:55

( ͡° ͜ʖ ͡°)

Name: Anonymous 2012-12-08 18:12

What can lazy evaluation do that currying can't?

Name: Anonymous 2012-12-09 13:30

>>21
It can't necrobump

Don't change these.
Name: Email:
Entire Thread Thread List