Re: [racket-users] Re: Implementing a lazy for/stream using for/fold/derived and delimited control

2015-05-20 Thread Matthew Flatt
At Tue, 19 May 2015 20:33:57 -0700, Alexis King wrote:
 As I've continued to experiment with this, I've run into something that I 
 don't 
 really understand. I've managed to come up with this snippet of code.
 
 (define (do prompt-tag)
   (define (loop element continue)
 (if continue
 (+ element (call-with-continuation-prompt continue prompt-tag loop))
 element))
   (call-with-continuation-prompt
(thunk
 (for/fold () ([i (in-range 10)])
   (let/cc continue
 (abort-current-continuation prompt-tag (let () i) continue)))
 (abort-current-continuation prompt-tag 0 #f))
prompt-tag loop))
 
 (do (default-continuation-prompt-tag))
 (do (make-continuation-prompt-tag))
 
 The first of the two calls at the end returns 45, but the second returns 0. 
 Why 
 does using a non-default prompt tag change the behavior if I'm installing my 
 own prompt anyway?

There's a `(default-continuation-prompt-tag)` lurking in `let/cc`. If
you change it to use `call/cc` and pass `prompt-tag` as the second
argument to `call/cc`, then you'll get the result that you expected.

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Implementing a lazy for/stream using for/fold/derived and delimited control

2015-05-19 Thread Alexis King
As I've continued to experiment with this, I've run into something that I don't 
really understand. I've managed to come up with this snippet of code.

(define (do prompt-tag)
  (define (loop element continue)
(if continue
(+ element (call-with-continuation-prompt continue prompt-tag loop))
element))
  (call-with-continuation-prompt
   (thunk
(for/fold () ([i (in-range 10)])
  (let/cc continue
(abort-current-continuation prompt-tag (let () i) continue)))
(abort-current-continuation prompt-tag 0 #f))
   prompt-tag loop))

(do (default-continuation-prompt-tag))
(do (make-continuation-prompt-tag))

The first of the two calls at the end returns 45, but the second returns 0. Why 
does using a non-default prompt tag change the behavior if I'm installing my 
own prompt anyway?


 On May 19, 2015, at 19:24, Alexis King lexi.lam...@gmail.com wrote:
 
 I'm trying to implement a for/stream loop using for/fold/derived that will 
 return a lazy stream, as would be expected. One way to do this is by using 
 delimited control, which is what I'm currently trying. If there's an easier 
 way, let me know, but I'd still like to figure this out as a pedagogical 
 exercise.
 
 Right now, I'm just trying to play with for/fold and some control operators 
 to get a feel for how a solution should work. Using racket/control, I've 
 managed to get this working snippet:
 
 (define result-stream
  (let ()
(define (stream-loop element continue)
  (stream-cons element
   (call-with-values
(thunk (call/prompt continue))
stream-loop)))
(call-with-values
 (thunk
  (prompt
   (for/fold ()
 ([i (in-naturals)])
 (let/cc continue
   (abort i continue)
 stream-loop)))
 
 This will create an infinite, lazy stream bound to ‘result-stream’ containing 
 all the natural numbers. It works, which is cool, but it also seems pretty 
 overcomplicated.
 
 Is there a better approach to this sort of thing that I'm missing? My 
 intuition for working with control operators isn't the best, so I wouldn't be 
 surprised if I was overlooking something.
 
 Thanks,
 Alexis

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.