lisp format总结

来源:互联网 发布:php http 异步请求 编辑:程序博客网 时间:2024/06/05 09:13

1: 基本格式化

(format destination control-string &rest format-arguments)

(输出目的 控制字符串 格式化参数)

 @,冒号 --> 修饰符

 ~A,~D  -->控制字符串

 V,#,十进制数字  --> 前置参数 (关键是寻找到底是用一个子句)

 ~A/~S 都可以使用任何类型的格式化参数,~S试图输出生成为可被read读回来的形式,所以字符串包含在引号内CL-USER> (format t "value is: ~S" "foo") value is: "foo"CL-USER> (format t "value is: ~A" "foo") value is: fooCL-USER> (format t "the value is: ~A" (list 1 2 3)) the value is: (1 2 3)CL-USER> (format t "the value is: ~S" (list 1 2 3)) the value is: (1 2 3)  读取整数十进制的~d,有两位的前置变量,第一个参数指输出最小宽度,第二个指占位符字符CL-USER> (format t "~d" 1000000) 1000000CL-USER> (format t "~:d" 1000000) 1,000,000CL-USER> (format t "~@:d" 1000000) +1,000,000CL-USER> (format t "~12d" 1000000) 1000000CL-USER> (format t "~12,'0d" 1000000)000001000000CL-USER> (format t "~4,'0d-~2,'0d-~2,'0d" 2005 6 10) 2005-06-10CL-USER> (format t "~x" 1000000) F4240CL-USER> (format t "~o" 1000000) 3641100CL-USER> (format t "~b" 1000000) 11110100001001000000    浮点共有四个指令:~f,~e,~g,~s,~f指令在数字特别大或特别小的时候允许使用科学计数法,~e指令在输出数字时总是使用科学计数法。  浮点指令~$ 等价于~,2f.如其名它是用于货币的,并且因为~5$中5表示小数点后位数,所以因为他可以有两个前置参数,第二个参数表控制十进制小数前所打印的最少位数.CL-USER> (format t "~$" pi) 3.14CL-USER> (format t "~5$" pi) 3.14159CL-USER> (format t "~2,4$" pi) 0003.14CL-USER> (format t "~f" pi) 3.141592653589793CL-USER> (format t "~,4f" pi) 3.1416CL-USER> (format t "~e" pi) 3.141592653589793d+0CL-USER> (format t "~,4e" pi) 3.1416d+0CL-USER> (format t "~2,4$" pi) 0003.14   转化为英文的数字表达形式CL-USER> (format t "~r" 1234) one thousand two hundred thirty-fourCL-USER> (format t "~:r" 1234)one thousand two hundred thirty-fourth   格式化参数不为1就输出S,表示复数啦.CL-USER> (format t "file~p" 1) fileCL-USER> (format t "file~p" 10)files   ~(另一个表示英文文本也就是string的指令,他必须与~)对应,下面第一个是直接用~a格式输出,它完全没有变化,而这个指令可以实现控制大小写.@将第一个单词首字母大写,冒号所有单词首字母大写,@与冒号同时   使用所有字母都大写CL-USER> (format t "~@a" "the Quick BROWN foX") the Quick BROWN foXCL-USER> (format t "~(~a~)" "FOO") fooCL-USER> (format t "~(~a~)" "the Quick BROWN foX") the quick brown foxCL-USER> (format t "~@(~a~)" "the Quick BROWN foX") The quick brown foxCL-USER> (format t "~:(~a~)" "the Quick BROWN foX") The Quick Brown FoxCL-USER> (format t "~:@(~a~)" "the Quick BROWN foX")THE QUICK BROWN FOX

Format directives can take arguments. ~F, which is used for printing right-justified floating-point numbers, can take up to five:

1. The total number of characters to be printed. Defaults to the exact length of the number.

2. The number of digits to print after the decimal. Defaults to all of them.

3. The number of digits to shift the decimal point to the left (thereby effectively multiplying the number by 10). Defaults to none.

4. The character to print instead of the number if it is too long to fit in the space allowed by the first argument. If no character is specified, an over-long number will be printed using as much space as it needs.当数字太大的时候,第一个参数指定的位数不够用

5. The character to print to the left before the digits start. Defaults to a blank.

CL-USER> (format nil "~10,2,0,'*,' f" 26.21875)"     26.22"CL-USER> (format nil "~10,2,1,0,' f" 26.2187512434343434434434)"    262.19"CL-USER> (format nil "~10,2,1,'8,' f" 262187512434.343434434434)"8888888888"CL-USER> (format nil "~10,2,1,'a,' f" 262187512434.343434434434)"aaaaaaaaaa"CL-USER> (format t "~10,2,1,'a,' f" 262187512434.343434434434)aaaaaaaaaaNIL

the first argument

If we give t as the first argument, output is sent to * standard- output*.

If we give nil, format returns as a string what it would have printed.

CL-USER> (format t "~S ~A" "z" "z")"z" zNILCL-USER> (format nil "~S ~A" "z" "z")"\"z\" z"

输出换行符

~% 总是产生换行符,可接受单个前置参数指定要产生的换行的个数。

~& 只在当前没有位于一行开始处时才产生换行,如果有前置参数会输出n-1或n个换行,取决与它是否在一行的开始处输出


~~ format输出波浪线,可以通过一个数字来参数化控制产生多少个波浪线

2: 条件格式化

~[对应格式~],在他们之间是有~;分割的句子。~[选取一个子句,然后让format处理,他一般有下面的形式决定到底是取那个句子。其中包含两个注意点:

1:千万别忘记写~]否则总是提示不能编译

2:最后一个子句分隔符是~:;表示最为默认的,如果没有这一个,下面100超界,将不会打印结果。

CL-USER> (format t "~[cero~;uno~;dos~:;mucho~]" 100)muchoCL-USER> (format t "~[cero~;uno~;dos~:;mucho~]" 2)dos
下面说的是#前置参数来决定到底是哪个子句让format处理,#表需要处理的参数的个数,既然是个数,所以应该是整数形式啦,下面例子比较长,看着挺乱,并且因为控制字符串中除转换指令以外都会被打印出来,各个子句是按~;来断句。所以为了方便起见我改为下面第二种形式,在各个~;前加了一个累加的表示数。
1:'a:需要处理参数个数为1,故第一个#为1得到a1,然后到第二个#,这时需要处理的个数为0,所以用第一项,最后结果为A14。注意当第二个为0时是最容易忽略的。
2: 'a 'b 'c:需要处理参数个数为3,所以按 ~a,~a~3,这一步处理了两个字符,剩下'c了,到第二个#就是1啦所以它是  and ~a5~ 
CL-USER> (defparameter *list-etc*     "~#[none~;~a~;~a and ~a:;~a,~a~]~#[~; and ~a~:;, ~a, etc~].")*LIST-ETC*CL-USER> (format t *list-etc*) none.CL-USER> (defparameter *list-etc*     "~#[none~;~a1~;~a and ~a2~:;~a,~a~3]~#[4~; and ~a5~:;, ~a, etc~].")*LIST-ETC*CL-USER> (format t *list-etc* 'a)             A14.              A.CL-USER> (format t *list-etc* 'a 'b)          A and B24.        A and b.CL-USER> (format t *list-etc* 'a 'b 'c)       A,B and C5.       A,B and CCL-USER> (format t *list-etc* 'a 'b 'c 'd)    A,B, C, etc.      A,B, C, etc.CL-USER> (format t *list-etc* 'a 'b 'c 'd 'e) A,B, C, etc.      A,B, C, etc.
3:使用冒号修饰符,~[将只含有两个子句,一个参数。当参数为nil是执行第一个子句,其他情况为第二个子句

CL-USER> (format t "~:[fail~;pass~]" test-result)

3: 迭代

~{与~}相对,参数必须是列表。只要被迭代列表上有元素,format将重复处理该控制字符串。
CL-USER> (format t "~{~a, ~}" (list 1 2 3))1, 2, 3,
你也许感觉上面结果中3后面的逗号太碍眼了,有没有办法去掉呢?~^在一个~{体内,表当列表中没有元素剩余时,~^令迭代立即停止无需处理后面控制字符。

CL-USER> (format t "~{~a~^, ~}" (list 1 2 3))1, 2, 3

~@{可以嵌入到~{ 或者~@{,并且未处理的元素,会一步步渗向底层,并且在最内层直至迭代结束。

1:(1)需要被处理参数为1,故第一个#为1用 ~A2~,处理完这个就没有元素了,你还有可能感觉因为上面~[时候,当没有元素的时候还是处理了后面的那个条件式。这点你要注意因为这个是嵌套的,并且当是最后一个元素它会把format中的那个子句给格式化完,不像~^马上就结束了。
2:(1 2 3 4)四个参数,用后面默认的即子迭代,处理1时#为3,故1,6然后迭代处理2,#为2得1,62, 6。
3:你会发现一旦他进入到子迭代中,对于列表的处理就不会再出去了,直到结束,例如当列表中为4个时,进入子列表,如果他会跳出来的话,当列表中少于3个时就该用外层的那个控制字符串啦。但是实际却不是这样,并且你仔细想一下也行,如果它嵌套了很多子迭代,肯定不会说当随着列表中元素的减少,然后它适合外部的子迭代然后跳到相应的地方再往下执行,这个太乱了。
CL-USER> (defparameter *english-list*     "~{~#[~;~A~;~A AND ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}")*ENGLISH-LIST*CL-USER> (format nil *english-list* '(1 2 3 4))"1, 2, 3, and 4"CL-USER> (defparameter *english-list*     "~{~#[1~;~A2~;~A AND ~a3~:;~@{~a~#[4~;, 5and ~:;, 6~]~}~]~}")*ENGLISH-LIST*CL-USER> (format nil *english-list* '())          ""                    ""CL-USER> (format nil *english-list* '(1))         "12"                  "1"CL-USER> (format nil *english-list* '(1 2))       "1 AND 23"            "1 and 2"CL-USER> (format nil *english-list* '(1 2 3))     "1, 62, 5and 34"      "1, 2, and 3" CL-USER> (format nil *english-list* '(1 2 3 4))   "1, 62, 63, 5and 44"  "1, 2, 3, and 4"


原创粉丝点击