[plt-scheme] Trying to reliably turn a value into a list of snips

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Thu May 29 12:50:01 EDT 2008

On Wed, 28 May 2008, Robby Findler wrote:

> Did you try just using `print' and then redirecting the output to your
> editor snip's ports?

Hi Robby,

But that's so simple that it can't possibly work... wait.  It works!

I hadn't known about print; I'd only been aware of printf, write, and 
display.  With your suggestion, my updated code is:

   ;; value->snips: value -> (listof snip%)
   (define (value->snips a-value)
     (let* ([text (new text%)]
            [port (open-output-text-editor text)])
       (print a-value port)
       (flush-output port)
       (get-text-snips text 0 (send text last-position))))

   ;; get-text-snips: text% number number -> (listof snip%)
   ;; Returns the snips in the text editor between start and end.
   (define (get-text-snips text start end)
     (send text split-snip start)
     (send text split-snip end)
      (let loop ([snips/rev '()]
                  (send text find-snip start 'after-or-none)])
          [(or (not a-snip)
               (>= (send text get-snip-position a-snip)
           (loop (cons (send a-snip copy) snips/rev)
                 (send a-snip next))]))))

and it deals with picts correctly.  Thanks again!

Posted on the users mailing list.