2009年2月9日月曜日

【実践CL】18 FORMATの手習い

formatについてこの章のように多様なサンプルを掲載している書籍は少ないと思う。そういう意味で貴重なもの。

ただしformatのある意味さわりを紹介しているにすぎない。がっちりやるならCLtL2か。
とりあえず、実践CLに沿って自分なりに早見表をまとめてみた。(実践CLまんまではない)

(format nil "~$" pi) ; "3.14"
(format nil "~5$" pi) ; "3.14159"
(format nil "~v$" 3 pi) ; "3.142"
(format nil "~#$" pi) ; "3.1"
(format nil "~#$" pi 'a) ; "3.14"
(format nil "~#$" pi 'a 'b) ; "3.142"
(format nil "~a ~#$" 'a pi 'b) ; "A 3.14"
(format nil "~2,4$" pi) ; "0003.14"

(format nil "~f" pi) ; "3.141592653589793d0"
(format nil "~,5f" pi) ; "3.14159"
(format nil "~e" pi) ; "3.141592653589793d+0"
(format nil "~,4e" pi) ; "3.1416d+0"
(format nil "~,4e" (* 10 pi)) ; "3.1416d+1"

(format nil "~d" 1000000) ; "1000000"
(format nil "~:d" 1000000) ; "1,000,000"
(format nil "~@d" 1000000) ; "+1000000"
(format nil "~@d" -1000000) ; "-1000000"
(format nil "~:@d" -1000000) ; "-1,000,000"
(format nil "~12d" 1000000) ; " 1000000"
(format nil "~12,'0d" 1000000) ; "000001000000"
(format nil "~4,'0d-~2,'0d-~2,'0d" 2005 6 10) ; "2005-06-10"
(format nil "~,,'.,4:d" 1000000) ; "100.0000"

(format nil "~x" 1000000) ; "f4240"
(format nil "~o" 1000000) ; "3641100"
(format nil "~b" 1000000) ; "11110100001001000000"


(format nil "The value is: ~a" 10) ; "The value is: 10"
(format nil "The value is: ~a" "foo") ; "The value is: foo"
(format nil "The value is: ~a" (list 1 2 3)) ; "The value is: (1 2 3)"

(format nil "Syntax error. Unexpected character: ~:c" #\a) ; "Syntax error. Unexpected character: a"
(format nil "~@c~%" #\a) ;"#\\a
;"
(format nil "~:@c" (code-char 0)) ; "null"

(format nil "~r" 1234) ; "one thousand two hundred thirty-four"
(format nil "~:r" 1234) ; "one thousand two hundred thirty-fourth"
(format nil "~@r" 1234) ; "MCCXXXIV"
(format nil "~:@r" 1234) ; "MCCXXXIIII"

(format nil "file~p" 1) ; "file"
(format nil "file~p" 10) ; "files"
(format nil "file~p" 0) ; "files"

(format nil "~r file~:p" 1) ; "one file"
(format nil "~r file~:p" 10) ; "ten files"
(format nil "~r file~:p" 0) ; "zero files"

(format nil "~r famil~:@p" 1) ; "one family"
(format nil "~r famil~:@p" 10) ; "ten families"
(format nil "~r famil~:@p" 0) ; "zero families"

(format nil "~(~a~) ~a" "AaA" "AaA") ; "aaa AaA"
(format nil "~(~a~)" "AaA bBb") ; "aaa bbb"
(format nil "~@(~a~)" "AaA bBb") ; "Aaa bbb"
(format nil "~:(~a~)" "AaA bBb") ; "Aaa Bbb"
(format nil "~:@(~a~)" "AaA bBb") ; "AAA BBB"

(format nil "~[cero~;uno~;dos~]" 0); "cero"
(format nil "~[cero~;uno~;dos~]" 1); "uno"
(format nil "~[cero~;uno~;dos~]" 2); "dos"
(format nil "~[cero~;uno~;dos~]" 3); ""

(format nil "~[cero~;uno~;dos~:;mucho~]" 3) ; "mucho"
(format nil "~[cero~;uno~;dos~:;mucho~]" 100) ; "mucho"

(defparameter *list-etc*
"~#[NONE~;~a~;~a and ~a~:;~a, ~a~]~#[~; and ~a~:;, ~a, etc~].")

(format nil *list-etc* ) ; "NONE."
(format nil *list-etc* 'a) ; "A."
(format nil *list-etc* 'a 'b) ; "A and B."
(format nil *list-etc* 'a 'b 'c) ; "A, B and C"
(format nil *list-etc* 'a 'b 'c 'd) ; "A, B, C, etc"
(format nil *list-etc* 'a 'b 'c 'd 'e) ; "A, B, C, etc"

(format nil "~:[FAIL~;pass~]" t); "pass"
(format nil "~:[FAIL~;pass~]" nil); "FAIL"

(format nil "~{~a, ~}" (list 1 2 3)) ; "1, 2, 3, "
(format nil "~{~a~^, ~}" (list 1 2 3)) ; "1, 2, 3"
(format nil "~@{~a~^, ~}" 1 2 3) ; "1, 2, 3"


(format nil "~r ~:*(~d)" 1) ; "one (1)"

(format nil "I saw ~r el~:*~[ves~;f~:;ves~]." 0); "I saw zero elves."
(format nil "I saw ~r el~:*~[ves~;f~:;ves~]." 1); "I saw one elf."
(format nil "I saw ~r el~:*~[ves~;f~:;ves~]." 2); "I saw two elves."

(format nil "~{~s~*~^ ~}" '(:a 10 :b 20)) ; ":A :B"

こつこつ。

0 件のコメント: