[plt-scheme] Scope question

From: Robby Findler (robby at cs.uchicago.edu)
Date: Fri Dec 28 15:54:30 EST 2007

You don't need "busy" as a variable to hold the threads. Just have
th-test return the threads in a list, like this:

(define (th-test lst)
  (map (lambda (x)
         (thread
          (lambda ()
            (begin (sleep x)
                   (display x)
                   (newline)))))
       lst))



> (th-test (list 1 2 3 4 5 6 7 8 8 9))
(#<thread> #<thread> #<thread> #<thread> #<thread> #<thread> #<thread>
#<thread> #<thread>)
1
2
3
4
5
6
7
8
9



On Dec 28, 2007 2:44 PM, Steven Kroeger <skroegs at gmail.com> wrote:
> Hi,
>
> I'm a scheme newb.
>
> I've got a procedure that generates threads in a loop. I'd like be
> able to access them out side that loop once they are generated. My
> procedure seems to work fine, but I'm not sure how to get at them.
>
> (define cp (list 1 2 3 4 5 6 7 8 9))
>
> (define busy '())
>
> (define (th-test lst)
>   (let loop ((vals lst))
>     (if (null? vals)
>         '()
>         (begin (append busy (cons (thread
>                                    (lambda ()
>                                      (let ((x (car vals)))
>                                        (begin (sleep x)
>                                               (display x)
>                                               (newline)))))
>                                   '()))
>                (loop (cdr vals))))))
>
> It generates the threads and outputs the numbers as its supposed to,
> but my  list "busy" never appears to have any threads in it.
>
> > (th-test cp)
> ()
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> >
>
> What would I have to do to to be able to see the created threads
> outside the creating loop?
>
> Thanks,
> Steve
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.