プログラミングGauche: P.202 練習問題

オブジェクトを文字列に変換するための手続きwrite-to-stringを、
call-with-output-stringを使って実装する問題。

gosh> (write-to-string '(1 "abc" "\"" #\z))
"(1 \"abc\" \"\\\"\" #\\z)"
gosh> (define (write-to-string2 obj)
(call-with-output-string (lambda (port)
(write obj port))))
write-to-string2
gosh> (write-to-string2 '(1 "abc" "\"" #\z))
"(1 \"abc\" \"\\\"\" #\\z)"


逆に文字列をオブジェクトへ変換

gosh> (define (read-from-string2 str)
(call-with-input-string str (lambda (x)
(read x))))
read-from-string2
gosh> (read-from-string "(1 \"abc\" \"\\\"\" #\\z)")
(1 "abc" "\"" #\z)
gosh> (read-from-string2 "(1 \"abc\" \"\\\"\" #\\z)")
(1 "abc" "\"" #\z)


cut版
(define (write-to-string2 obj)
(call-with-output-string (cut write obj <>)))

(define (read-from-string2 str)
(call-with-input-string str (cut read <>)))

コメント

人気の投稿