Common Lisp HyperSpec のシンボルを眺めたり評価したりしています。今日は write-to-string です。
----------------------------------------------------------------------------------------------------------------------------------------------------------
Function WRITE-TO-STRING, PRIN1-TO-STRING, PRINC-TO-STRING
Syntax:
write-to-string object &key array base case circle escape gensym length level lines miser-width pprint-dispatch pretty radix readably right-margin
=> string
prin1-to-string object => string
princ-to-string object => string
Arguments and Values:
object---an object.
array---a generalized boolean.
base---a radix.
case---a symbol of type (member :upcase :downcase :capitalize).
circle---a generalized boolean.
escape---a generalized boolean.
gensym---a generalized boolean.
length---a non-negative integer, or nil.
level---a non-negative integer, or nil.
lines---a non-negative integer, or nil.
miser-width---a non-negative integer, or nil.
pprint-dispatch---a pprint dispatch table.
pretty---a generalized boolean.
radix---a generalized boolean.
readably---a generalized boolean.
right-margin---a non-negative integer, or nil.
string---a string.
Description:
write-to-string, prin1-to-string, and princ-to-string are used to create a string consisting of the printed representation of object. Object is
effectively printed as if by write, prin1, or princ, respectively, and the characters that would be output are made into a string.
write-to-string is the general output function. It has the ability to specify all the parameters applicable to the printing of object.
prin1-to-string acts like write-to-string with :escape t, that is, escape characters are written where appropriate.
princ-to-string acts like write-to-string with :escape nil :readably nil. Thus no escape characters are written.
All other keywords that would be specified to write-to-string are default values when prin1-to-string or princ-to-string is invoked.
The meanings and defaults for the keyword arguments to write-to-string are the same as those for write.
Examples:
(prin1-to-string "abc") => "\"abc\""
(princ-to-string "abc") => "abc"
Side Effects: None.
Affected By:
*print-escape*, *print-radix*, *print-base*, *print-circle*, *print-pretty*, *print-level*, *print-length*, *print-case*, *print-gensym*, *print-array*,
*read-default-float-format*.
Exceptional Situations: None.
See Also:
write
Notes:
(write-to-string object {key argument}*)
== (with-output-to-string (#1=#:string-stream)
(write object :stream #1# {key argument}*))
(princ-to-string object)
== (with-output-to-string (string-stream)
(princ object string-stream))
(prin1-to-string object)
== (with-output-to-string (string-stream)
(prin1 object string-stream))
----------------------------------------------------------------------------------------------------------------------------------------------------------
The following X3J13 cleanup issue, not part of the specification, applies to this section:
* PRINC-READABLY:X3J13-DEC-91
----------------------------------------------------------------------------------------------------------------------------------------------------------
文字列を出力する関数のようです。 prin1-to-string princ-to-string は write-to-string のオプションが一部設定済みになったシノニムみたいですね。
評価してみましたが escape まわりの挙動がよくわかりませんでした。
CL-USER> (write-to-string "Hello.\nI am Nancy.") "\"Hello.nI am Nancy.\"" CL-USER> (setq ret (write-to-string "Hello.\nI am Nancy.")) ; in: LAMBDA NIL ; (SETQ RET (WRITE-TO-STRING "Hello.nI am Nancy.")) ; ; caught WARNING: ; undefined variable: RET ; ; compilation unit finished ; Undefined variable: ; RET ; caught 1 WARNING condition "\"Hello.nI am Nancy.\"" CL-USER> ret "\"Hello.nI am Nancy.\""
"\n" は改行文字を意図してますが、印字された文字列は改行されませんでした。あと Side Effects: None.
と書いてあるので評価した返り値を変数に入れたあとで変数を評価しても nil とかになるのではないかと期待したのですが、文字列が印字されました。なんでだろう。