使用语法->字符串将列表列表转换为字符串列表

发布于 2024-11-01 17:25:53 字数 768 浏览 1 评论 0原文

基本上,我想要 '( (whatever1) (whatever2) (whatever3) ... ) ===> ( "(whatever1)" "(whatever2)" "(whatever3)" ),只是在列表之外添加引号,列表中的内容保持不变。例如

'((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)))")

所以问题是:不再有空格了! 我该如何解决这个问题?

Basically, I want '( (whatever1) (whatever2) (whatever3) ... ) ===> ( "(whatever1)" "(whatever2)" "(whatever3)" ), which is just add quotes outside of the list, 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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-11-08 17:25:53
#lang racket
(define lst-sub '((define x :: int) (=> T (and (= X X) (= 0 0)))))
(pretty-write (map (λ (x) (format "~s" x)) lst-sub))
#lang racket
(define lst-sub '((define x :: int) (=> T (and (= X X) (= 0 0)))))
(pretty-write (map (λ (x) (format "~s" x)) lst-sub))
影子是时光的心 2024-11-08 17:25:53

好吧。我不走我认为的“简单”路线。并按如下方式计算,最终会产生更多行代码:(

(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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文