Debugging 将方案对象序列化为字符串

Debugging 将方案对象序列化为字符串,debugging,scheme,r6rs,Debugging,Scheme,R6rs,执行(显示obj)时,输出将显示一个良好的表示但是否可以将此表示形式捕获到字符串? 我可以用它来更好地处理调试信息 我能得到的最接近的方法是将对象显示为.txt,然后将其作为字符串读回: (define (to-string obj) (call-with-output-file "to-string.txt" (lambda (output-port) (display obj output-port))) (call-with-input-file "to-string.txt" (

执行(显示obj)时,输出将显示一个良好的表示但是否可以将此表示形式捕获到字符串? 我可以用它来更好地处理调试信息

我能得到的最接近的方法是将对象显示为.txt,然后将其作为字符串读回:

(define (to-string obj)

(call-with-output-file "to-string.txt"
(lambda (output-port)
  (display obj output-port)))

(call-with-input-file "to-string.txt"
(lambda (input-port)
  (define str "")
  (let loop ((x (read-char input-port)))
    (if (not (eof-object? x))
        (begin
          (set! str (string-append str (string x)))
          (loop (read-char input-port))))
    str)))
)
(define obj (cons "test" (make-vector 3)))
(define str (to-string obj))
; str will contain "{test . #(0 0 0)}"

感谢@soegaard!找到了答案

(define (to-string obj)
  (define q (open-output-string))
  (write obj q)
  (get-output-string q)
)
(define obj (cons "test" (make-vector 3)))
(define str (to-string obj))
; str will contain ("test" . #(0 0 0))

你对r6rs的立场是什么?我可以鼓励你透露你正在使用什么实现吗?寻找开放输出stringHey John,我在学校使用DrRacket。我想制作一个工具,以图形方式显示scheme对象,比如二叉树,或者一些实验性垃圾收集代码的内存。我将使用
(send url“”)将scheme对象发送到我的图形代码中