convert a list of data structure in string, scheme

喜欢而已 提交于 2019-12-25 07:39:20

问题


for example I have this structure:

(define-struct example (n1 n2)) 

and I have this list:

(list (make-example 1 3) (make-example 7 9) empty)

As I can convert it into string?


回答1:


Since you have tagged both racket and scheme (two incompatible languages) I've ignored Scheme completely in my answer. I assume you would not tag racket if you were programming in #!r5rs or #!r6rs.

#!racket

(define (any->string any) 
  (with-output-to-string (lambda () (write any))))

(any->string '(Hello world)) ; ==> "(Hello world)"



回答2:


Racket already has the ~ a procedure to do exactly that:

> (~a '(Hello world))
"(Hello world)"

> (define-struct example (n1 n2) #:transparent) 
> (define l (list (make-example 1 3) (make-example 7 9) empty))
> (~a l)
"(#(struct:example 1 3) #(struct:example 7 9) ())"

Also, note that ~a has tons of options for formatting which may come in handy.



来源:https://stackoverflow.com/questions/24080685/convert-a-list-of-data-structure-in-string-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!