convert a list of data structure in string, scheme

2019-09-09 02:17发布

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?

标签: scheme racket
2条回答
ゆ 、 Hurt°
2楼-- · 2019-09-09 03:09

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.

查看更多
叛逆
3楼-- · 2019-09-09 03:18

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 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)"
查看更多
登录 后发表回答