Turning a list of list to list of string using syntax->string
Basically, I want '( (whatever1) (whatever2) (whatever3) ... )
===> ( "(whatever1)" "(whatever2)" "(whatever3)" )
, which is just add quotes outside of the list, 开发者_StackOverflow社区and keep the contents in the list unchanged. e.g.
'((define X ::int)
(define b0 :: bool (=> T (and (= X X) (= 0 0)))))
will be turned into:
'("(define X ::int)"
"(define b0 :: bool (=> T (and (= X X) (= 0 0))))")
However, the following code I am using eliminate all spaces!
#lang racket
(require syntax/to-string)
(define lst-sub '((define x :: int) (=> T (and (= X X) (= 0 0)))))
(pretty-write (map (λ (x) (string-append "(" (syntax->string (datum->syntax #f x)) ")")) lst-sub))
which returns
("(definex::int)" "(=>T(and(=XX)(=00)))")
So the question is: there is no spaces anymore! How can I get around this??
#lang racket
(define lst-sub '((define x :: int) (=> T (and (= X X) (= 0 0)))))
(pretty-write (map (λ (x) (format "~s" x)) lst-sub))
Alright. I don't take the "easy" route I thought. and worked out as follows, which ends up with more lines of code :(
(define (toString-with-space data)
(match data
[(? symbol?) (string-append (symbol->string data) " ")]
[(? number?) (string-append (number->string data) " ")]))
(define (flat-def def-lst)
(if (empty? def-lst)
(list)
(begin
(let ([f (car def-lst)])
(if (not (list? f))
(cons (toString-with-space f) (flat-def (drop def-lst 1)))
(append (list "(") (flat-def f) (flat-def (drop def-lst 1)) (list ")")))))))
(define (lstStr->lstChars lst-str)
(for/fold ([l empty])
([el (in-list lst-str)])
(append l (string->list el))))
(define flat (flat-def ' (define b1 :: bool (=> (and (= X x) (= Y y)) (and (= Y y) (= X x))))))
(set! flat (append (list "\"" "(") flat (list ")" "\"")))
(set! flat (lstStr->lstChars flat))
(set! flat (list->string flat))
(display flat)
精彩评论