lisp 里的format
是输出的利器,不亚于c 语言的printf()
我们主要展示 控制字符的行为:
注意 ~s 是输出 “” 的 ~a 是不输出的。
~10s ~10a 指定宽带,空格后置
~10@a ~10@s 指定宽带,空格前置
CL-USER> (format t "[~10a]" 'abc)
[ABC]
NIL
CL-USER> (format t "[~10a]" 'abc)
[ABC ]
NIL
CL-USER> (format t "[~10s]" 'abc)
[ABC ]
NIL
CL-USER> (format t "[~10s]" "abc")
["abc" ]
NIL
CL-USER> (format t "[~10a " "abc")
[abc
NIL
CL-USER> (format t "[~10@a]" "abc")
[ abc]
NIL
CL-USER> (format t "[~10@s]" "abc")
[ "abc"]
NIL
CL-USER>
再看一个
CL-USER> (format t "[~10,4a]" "abc")
[abc ]
NIL
数一下【】 一共是11个字符,跟我们之前的说的不一致了,什么问题呢?
后面的4 这个参数,就是活每次扩展4个占位符
我们定义了10的宽度,去掉abc 3个宽带,还剩7个字符,一次补充4个,那么7个字符就要补充两次,就变成8个了,3+8=11 了。
明白了吧!!
继续下一个例子:
CL-USER> (format t "[~8,3,,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~9,3,,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~9,3,4,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~10,3,4,'!a]" "abc")
[abc!!!!!!!]
NIL
CL-USER> (format t "[~10,3,4,'!a]" "abc")
[abc!!!!!!!]
NIL
这个模式可以指定填充用的字符。
---==================华丽分隔
下面这些就是数字的输出格式了
CL-USER> (format t " 1000 is x:[~x] " 1000)
1000 is x: 3E8
NIL
CL-USER> (format t " 1000 is x:[~x] " 1000)
1000 is x:[3E8]
NIL
CL-USER> (format t "1000 is binary: [~b]" 1000)
1000 is binary: [1111101000]
NIL
CL-USER> (format t "1000 is dicemal : [~d]" 1000)
1000 is dicemal : [1000]
NIL
CL-USER> (format t "1000 is float : [~f]" 1000)
1000 is float : [1000.0]
NIL
CL-USER> (format t "1000 is dicemal : [~:d]" 1000)
1000 is dicemal : [1,000]
CL-USER> (format t "1000 is dicemal : [~10d]" 1000)
1000 is dicemal : [ 1000]
NIL
CL-USER> (format t "1000 is dicemal : [~10,xd]" 1000)
1000 is dicemal : [ 3E8d]
NIL
CL-USER>
; No value
CL-USER> (format t "1000 is dicemal : [~10,'xd]" 1000)
1000 is dicemal : [xxxxxx1000]
NIL
CL-USER> (format t "[~4f]" 3.1415926)
[3.14]
NIL
CL-USER> (format t "[~6f]" 3.1415926)
[3.1416]
NIL
CL-USER>
; No value
CL-USER>
CL-USER> (format t "percent : [~,,2f]" 0.77)
percent : [77.0]
NIL
CL-USER> (format t "percent : [~,,2f%]" 0.77)
percent : [77.0%]
NIL
CL-USER> (format t "$2.3 is [~$]" 2.3)
$2.3 is [2.30]
NIL
CL-USER>
恩,数字就先这些吧,
================
下面看看控制字符换行符
~%
~&
CL-USER> (format t " newline ~% and ~&" )
newline
and
NIL
CL-USER> (format t " newline ~% and ~& xxx" )
newline
and
xxx
NIL
CL-USER>
; No value
CL-USER> (format t " newline ~& and ~% xxx" )
newline
and
xxx
NIL
CL-USER> (format t " newline ~& and ~%" )
newline
and
NIL
CL-USER>
~% 是一定要给一个新行的
~& 则不一定每次都给一个新行,只在必要的时候给一个。
CL-USER> (progn
(format t " newline and ~%" )
(format t "~& newline and ~%")
)
newline and
newline and
NIL
CL-USER> (progn
(format t " newline and ~%" )
(format t "~% newline and ~%")
)
newline and
newline and
NIL
CL-USER>
======================
继续
~t 是定义列 里面的数字跟前面一个意思
CL-USER> (defun random-animal ()
(nth (random 5) '("dog" "tick" "tiger" "walrus" "kangaroo")))
RANDOM-ANIMAL
CL-USER> (loop repeat 10
do (format t "~5t~a ~15t~a ~25t~a~%"
(random-animal)
(random-animal)
(random-animal)))
kangaroo dog kangaroo
kangaroo dog kangaroo
kangaroo tick walrus
tick dog tiger
tick dog tiger
kangaroo kangaroo kangaroo
kangaroo kangaroo dog
walrus kangaroo kangaroo
kangaroo dog tiger
walrus kangaroo tick
NIL
在继续
~30 标识构建一个30字符宽的块(block)
CL-USER> (loop repeat 10
do (format t "~30~%"
(random-animal)
(random-animal)
(random-animal)))
dog dog tiger
dog kangaroo walrus
walrus kangaroo kangaroo
tiger walrus walrus
dog tick kangaroo
tick kangaroo walrus
walrus tick walrus
walrus walrus tiger
tick tiger walrus
dog dog tick
NIL
=====================
再继续 :
:@ 意思是居中打印
CL-USER> (loop repeat 10
do (format t "~30:@~%"
(random-animal)
(random-animal)
(random-animal)))
tiger tiger tiger
tiger walrus walrus
walrus kangaroo walrus
kangaroo tick walrus
walrus dog walrus
walrus dog walrus
dog tiger tiger
walrus kangaroo tiger
tick kangaroo tiger
kangaroo tick dog
NIL
CL-USER>
====================
再继续 :
这个一看就明白了,不多说了
CL-USER> (defparameter *animals* (loop repeat 10 collect (random-animal)))
*ANIMALS*
CL-USER> *animals*
("walrus" "kangaroo" "tick" "kangaroo" "tiger" "kangaroo" "walrus" "tiger"
"kangaroo" "tiger")
CL-USER> (format t "~{I see a ~a! ~}" *animals*)
I see a walrus! I see a kangaroo! I see a tick! I see a kangaroo! I see a tiger! I see a kangaroo! I see a walrus! I see a tiger! I see a kangaroo! I see a tiger!
NIL
CL-USER> (format t "~{I see a ~a!~% ~}" *animals*)
I see a walrus!
I see a kangaroo!
I see a tick!
I see a kangaroo!
I see a tiger!
I see a kangaroo!
I see a walrus!
I see a tiger!
I see a kangaroo!
I see a tiger!
CL-USER> (format t "~{I see a ~a... or was it a ~a?~%~}" *animals*)
I see a walrus... or was it a kangaroo?
I see a tick... or was it a kangaroo?
I see a tiger... or was it a kangaroo?
I see a walrus... or was it a tiger?
I see a kangaroo... or was it a tiger?
====================
再来一个bt 一点的
CL-USER> (format t "|~{~~}|" (loop for x below 100 collect x))
| 0 1 2 3 4 5 6 7 8 9 |
|10 11 12 13 14 15 16 17 18 19 |
|20 21 22 23 24 25 26 27 28 29 |
|30 31 32 33 34 35 36 37 38 39 |
|40 41 42 43 44 45 46 47 48 49 |
|50 51 52 53 54 55 56 57 58 59 |
|60 61 62 63 64 65 66 67 68 69 |
|70 71 72 73 74 75 76 77 78 79 |
|80 81 82 83 84 85 86 87 88 89 |
|90 91 92 93 94 95 96 97 98 99 |
NIL
说明一下
~{ ~} 这一对是循环 ,对应于loop 里面的控制字符是要循环控制的
~< ~> 这个前面看过了,生成一个块结构
|~%| 新行 没有问题
~:; 这个也是新行 ,前面那个33 是是说,这一行的长度到了33字符,就要换行了。
~2d 这个没得说了 2位数字。
好了,也差不多了,够长的了。
是输出的利器,不亚于c 语言的printf()
我们主要展示 控制字符的行为:
注意 ~s 是输出 “” 的 ~a 是不输出的。
~10s ~10a 指定宽带,空格后置
~10@a ~10@s 指定宽带,空格前置
CL-USER> (format t "[~10a]" 'abc)
[ABC]
NIL
CL-USER> (format t "[~10a]" 'abc)
[ABC ]
NIL
CL-USER> (format t "[~10s]" 'abc)
[ABC ]
NIL
CL-USER> (format t "[~10s]" "abc")
["abc" ]
NIL
CL-USER> (format t "[~10a " "abc")
[abc
NIL
CL-USER> (format t "[~10@a]" "abc")
[ abc]
NIL
CL-USER> (format t "[~10@s]" "abc")
[ "abc"]
NIL
CL-USER>
再看一个
CL-USER> (format t "[~10,4a]" "abc")
[abc ]
NIL
数一下【】 一共是11个字符,跟我们之前的说的不一致了,什么问题呢?
后面的4 这个参数,就是活每次扩展4个占位符
我们定义了10的宽度,去掉abc 3个宽带,还剩7个字符,一次补充4个,那么7个字符就要补充两次,就变成8个了,3+8=11 了。
明白了吧!!
继续下一个例子:
CL-USER> (format t "[~8,3,,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~9,3,,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~9,3,4,'!a]" "abc")
[abc!!!!!!]
NIL
CL-USER> (format t "[~10,3,4,'!a]" "abc")
[abc!!!!!!!]
NIL
CL-USER> (format t "[~10,3,4,'!a]" "abc")
[abc!!!!!!!]
NIL
这个模式可以指定填充用的字符。
---==================华丽分隔
下面这些就是数字的输出格式了
CL-USER> (format t " 1000 is x:[~x] " 1000)
1000 is x: 3E8
NIL
CL-USER> (format t " 1000 is x:[~x] " 1000)
1000 is x:[3E8]
NIL
CL-USER> (format t "1000 is binary: [~b]" 1000)
1000 is binary: [1111101000]
NIL
CL-USER> (format t "1000 is dicemal : [~d]" 1000)
1000 is dicemal : [1000]
NIL
CL-USER> (format t "1000 is float : [~f]" 1000)
1000 is float : [1000.0]
NIL
CL-USER> (format t "1000 is dicemal : [~:d]" 1000)
1000 is dicemal : [1,000]
CL-USER> (format t "1000 is dicemal : [~10d]" 1000)
1000 is dicemal : [ 1000]
NIL
CL-USER> (format t "1000 is dicemal : [~10,xd]" 1000)
1000 is dicemal : [ 3E8d]
NIL
CL-USER>
; No value
CL-USER> (format t "1000 is dicemal : [~10,'xd]" 1000)
1000 is dicemal : [xxxxxx1000]
NIL
CL-USER> (format t "[~4f]" 3.1415926)
[3.14]
NIL
CL-USER> (format t "[~6f]" 3.1415926)
[3.1416]
NIL
CL-USER>
; No value
CL-USER>
CL-USER> (format t "percent : [~,,2f]" 0.77)
percent : [77.0]
NIL
CL-USER> (format t "percent : [~,,2f%]" 0.77)
percent : [77.0%]
NIL
CL-USER> (format t "$2.3 is [~$]" 2.3)
$2.3 is [2.30]
NIL
CL-USER>
恩,数字就先这些吧,
================
下面看看控制字符换行符
~%
~&
CL-USER> (format t " newline ~% and ~&" )
newline
and
NIL
CL-USER> (format t " newline ~% and ~& xxx" )
newline
and
xxx
NIL
CL-USER>
; No value
CL-USER> (format t " newline ~& and ~% xxx" )
newline
and
xxx
NIL
CL-USER> (format t " newline ~& and ~%" )
newline
and
NIL
CL-USER>
~% 是一定要给一个新行的
~& 则不一定每次都给一个新行,只在必要的时候给一个。
CL-USER> (progn
(format t " newline and ~%" )
(format t "~& newline and ~%")
)
newline and
newline and
NIL
CL-USER> (progn
(format t " newline and ~%" )
(format t "~% newline and ~%")
)
newline and
newline and
NIL
CL-USER>
======================
继续
~t 是定义列 里面的数字跟前面一个意思
CL-USER> (defun random-animal ()
(nth (random 5) '("dog" "tick" "tiger" "walrus" "kangaroo")))
RANDOM-ANIMAL
CL-USER> (loop repeat 10
do (format t "~5t~a ~15t~a ~25t~a~%"
(random-animal)
(random-animal)
(random-animal)))
kangaroo dog kangaroo
kangaroo dog kangaroo
kangaroo tick walrus
tick dog tiger
tick dog tiger
kangaroo kangaroo kangaroo
kangaroo kangaroo dog
walrus kangaroo kangaroo
kangaroo dog tiger
walrus kangaroo tick
NIL
在继续
~30 标识构建一个30字符宽的块(block)
CL-USER> (loop repeat 10
do (format t "~30~%"
(random-animal)
(random-animal)
(random-animal)))
dog dog tiger
dog kangaroo walrus
walrus kangaroo kangaroo
tiger walrus walrus
dog tick kangaroo
tick kangaroo walrus
walrus tick walrus
walrus walrus tiger
tick tiger walrus
dog dog tick
NIL
=====================
再继续 :
:@ 意思是居中打印
CL-USER> (loop repeat 10
do (format t "~30:@~%"
(random-animal)
(random-animal)
(random-animal)))
tiger tiger tiger
tiger walrus walrus
walrus kangaroo walrus
kangaroo tick walrus
walrus dog walrus
walrus dog walrus
dog tiger tiger
walrus kangaroo tiger
tick kangaroo tiger
kangaroo tick dog
NIL
CL-USER>
====================
再继续 :
这个一看就明白了,不多说了
CL-USER> (defparameter *animals* (loop repeat 10 collect (random-animal)))
*ANIMALS*
CL-USER> *animals*
("walrus" "kangaroo" "tick" "kangaroo" "tiger" "kangaroo" "walrus" "tiger"
"kangaroo" "tiger")
CL-USER> (format t "~{I see a ~a! ~}" *animals*)
I see a walrus! I see a kangaroo! I see a tick! I see a kangaroo! I see a tiger! I see a kangaroo! I see a walrus! I see a tiger! I see a kangaroo! I see a tiger!
NIL
CL-USER> (format t "~{I see a ~a!~% ~}" *animals*)
I see a walrus!
I see a kangaroo!
I see a tick!
I see a kangaroo!
I see a tiger!
I see a kangaroo!
I see a walrus!
I see a tiger!
I see a kangaroo!
I see a tiger!
CL-USER> (format t "~{I see a ~a... or was it a ~a?~%~}" *animals*)
I see a walrus... or was it a kangaroo?
I see a tick... or was it a kangaroo?
I see a tiger... or was it a kangaroo?
I see a walrus... or was it a tiger?
I see a kangaroo... or was it a tiger?
====================
再来一个bt 一点的
CL-USER> (format t "|~{~~}|" (loop for x below 100 collect x))
| 0 1 2 3 4 5 6 7 8 9 |
|10 11 12 13 14 15 16 17 18 19 |
|20 21 22 23 24 25 26 27 28 29 |
|30 31 32 33 34 35 36 37 38 39 |
|40 41 42 43 44 45 46 47 48 49 |
|50 51 52 53 54 55 56 57 58 59 |
|60 61 62 63 64 65 66 67 68 69 |
|70 71 72 73 74 75 76 77 78 79 |
|80 81 82 83 84 85 86 87 88 89 |
|90 91 92 93 94 95 96 97 98 99 |
NIL
说明一下
~{ ~} 这一对是循环 ,对应于loop 里面的控制字符是要循环控制的
~< ~> 这个前面看过了,生成一个块结构
|~%| 新行 没有问题
~:; 这个也是新行 ,前面那个33 是是说,这一行的长度到了33字符,就要换行了。
~2d 这个没得说了 2位数字。
好了,也差不多了,够长的了。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/133735/viewspace-752267/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/133735/viewspace-752267/