Shammer's Philosophy

My private adversaria

with-standard-io-syntaxってどんな意味が・・・

CommonLisp Hyper Specによれば、with-standard-io-syntaxは以下のように定義されている。

Within the dynamic extent of the body of forms, all reader/printer control variables, including any implementation-defined ones not specified by this standard, are bound to values that produce standard read/print behavior. The values for the variables specified by this standard are listed in the next figure.

これだと正直よくわからない。なので、例から判断する。

(with-open-file (file pathname :direction :output)
   (with-standard-io-syntax
     (print data file)))

まずこれは、dataという何らかの情報をfileに書き出す処理だが、ここでwith-standard-io-syntaxを使用している。dataは何らかのリストだかatomだかだ。この例に合わせて以下のように実行。

? (with-open-file (file "test.l" :direction :output :if-exists :supersede)
    (with-standard-io-syntax
      (print (list 10 20 30) file)))
(10 20 30)
? 

test.lは以下のようになった。

$ cat test.l

(10 20 30) 
$

Lispのリストとして保存されている。これを試しにwith-standard-io-syntaxなしでやってみる。

? (with-open-file (file "test.l" :direction :output :if-exists :supersede)
    (print (list 10 20 30) file))
(10 20 30)
? 

test.lは以下のようになった。

$ cat test.l

(10 20 30) 
$ 

違いがわからないが。とりあえず、これを例にあるように読み込む。

? (with-open-file (file "test.l" :direction :input)
    (with-standard-io-syntax
      (defparameter data (read file))))
DATA
? data
(10 20 30)
? 

書き出した内容を、そのままLispの式として変数にセットした、というところだろうか。with-standard-io-syntaxなしでやるとどうなるのか。

? (with-open-file (file "test.l" :direction :input)
    (defparameter data2 (read file)))
DATA2
? data2
(10 20 30)
? 

まったくもってわからなくなってしまった。あってもなくても何も変わらないように見える。もう少し複雑な構文でやってみないとダメなのかもしれない。