基本上,我想要'( (whatever1) (whatever2) (whatever3) ... )
===> ( "(whatever1)" "(whatever2)" "(whatever3)" )
,只是在列表外添加引号,并保持列表中的内容不变。 e.g。
'((define X ::int)
(define b0 :: bool (=> T (and (= X X) (= 0 0)))))
将变为:
'("(define X ::int)"
"(define b0 :: bool (=> T (and (= X X) (= 0 0))))")
但是,我使用的以下代码消除了所有空格!
#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))
返回
("(definex::int)" "(=>T(and(=XX)(=00)))")
所以问题是:再也没有空格了! 我怎么能绕过这个?
答案 0 :(得分:2)
#lang racket
(define lst-sub '((define x :: int) (=> T (and (= X X) (= 0 0)))))
(pretty-write (map (λ (x) (format "~s" x)) lst-sub))
答案 1 :(得分:1)
好的。我没有采取我认为的“简单”路线。并计算如下,最终得到更多代码:(
(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)