打印原始控制字符,同时在输出中保留字符串定界符 (Racket)
Print raw control characters while preserving string delimiters in output (Racket)
我在 Racket 中有一个这样的列表:
'(some-symbol
"some\ntext\ngoes in\nhere")
我想将其打印出来,以便将 \n
等控制字符转换为它们的实际值,在本例中为换行符。但是,我还希望在输出中保留字符串的引号(即分隔符)la write
或 print
。 display
函数已经做了我想要的第一部分,但它去掉了像 \"
那样没有转义的引号。例如:
racket@> (displayln '(some-symbol "some\ntext\ngoes in\nhere")) ;; I want the linefeeds as produced here
(some-symbol some
text
goes in
here)
racket@> (println '(some-symbol "some\ntext\ngoes in\nhere")) ;; But I also want the quotation marks as preserved here
'(some-symbol "some\ntext\ngoes in\nhere")
racket@>
有没有什么方法可以在 Racket 中获得这种输出效果而无需转义 \"
之类的字符串定界符?另外,我不希望输出中列表前面的 '
字符。
不清楚你到底想要什么,所以让我们为你安排一个稻草人bash:
#lang racket
(require rackunit)
;; find every string in an s-expression, add quotes to it:
(define (add-quotes s)
(cond [(list? s)
(map add-quotes s)]
[(string? s)
(string-append "\"" s "\"")]
[else s]))
(check-equal? (add-quotes '((a b "cde") (("g") f)))
'((a b "\"cde\"") (("\"g\"") f)))
;; display s with quotes around strings:
(define (funny-display s)
(display (add-quotes s)))
不是你想要的?
我在 Racket 中有一个这样的列表:
'(some-symbol
"some\ntext\ngoes in\nhere")
我想将其打印出来,以便将 \n
等控制字符转换为它们的实际值,在本例中为换行符。但是,我还希望在输出中保留字符串的引号(即分隔符)la write
或 print
。 display
函数已经做了我想要的第一部分,但它去掉了像 \"
那样没有转义的引号。例如:
racket@> (displayln '(some-symbol "some\ntext\ngoes in\nhere")) ;; I want the linefeeds as produced here
(some-symbol some
text
goes in
here)
racket@> (println '(some-symbol "some\ntext\ngoes in\nhere")) ;; But I also want the quotation marks as preserved here
'(some-symbol "some\ntext\ngoes in\nhere")
racket@>
有没有什么方法可以在 Racket 中获得这种输出效果而无需转义 \"
之类的字符串定界符?另外,我不希望输出中列表前面的 '
字符。
不清楚你到底想要什么,所以让我们为你安排一个稻草人bash:
#lang racket
(require rackunit)
;; find every string in an s-expression, add quotes to it:
(define (add-quotes s)
(cond [(list? s)
(map add-quotes s)]
[(string? s)
(string-append "\"" s "\"")]
[else s]))
(check-equal? (add-quotes '((a b "cde") (("g") f)))
'((a b "\"cde\"") (("\"g\"") f)))
;; display s with quotes around strings:
(define (funny-display s)
(display (add-quotes s)))
不是你想要的?