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

2015-05-20 Thread Konrad Hinsen

On 20/05/2015 04:24, Alexis King 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.


If I understand correctly what you are trying to do, the simplest 
solution seems to be a generator.


=
#lang racket

(require racket/generator)

(define result-stream
  (in-generator
   (for ([i (in-naturals 10)])
 (yield i

(for ([n result-stream])
  (printf ~s\n n))
=

This example is a bit pointless because result-stream could just as well 
be replaced by (in-naturals 10), but I hope it is clear how this could 
be generalized to a meaningful application.


I use this a lot, to the point of considering defining something like 
for/sequence to have nicer syntax for this.


Konrad.

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


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

2015-05-20 Thread Alexis King
I've never particularly loved racket/generator's interface, but you're right 
that it could be a useful base for this sort of abstraction. I do explicitly 
want a stream, but I could use in-generator in tandem with sequence-stream to 
get that.

That said, I would still be interested in figuring out how to roll it myself 
using a solution similar to what I proposed for learning purposes.

 On May 19, 2015, at 23:11, Konrad Hinsen konrad.hin...@fastmail.net wrote:
 
 On 20/05/2015 04:24, Alexis King 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.
 
 If I understand correctly what you are trying to do, the simplest solution 
 seems to be a generator.
 
 =
 #lang racket
 
 (require racket/generator)
 
 (define result-stream
  (in-generator
   (for ([i (in-naturals 10)])
 (yield i
 
 (for ([n result-stream])
  (printf ~s\n n))
 =
 
 This example is a bit pointless because result-stream could just as well be 
 replaced by (in-naturals 10), but I hope it is clear how this could be 
 generalized to a meaningful application.
 
 I use this a lot, to the point of considering defining something like 
 for/sequence to have nicer syntax for this.
 
 Konrad.

-- 
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] Implementing a lazy for/stream using for/fold/derived and delimited control

2015-05-19 Thread Alexis King
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.