lisp 利器 format 語法示例 控制字元

babyyellow發表於2013-01-07
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位數字。



好了,也差不多了,夠長的了。





來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-752267/,如需轉載,請註明出處,否則將追究法律責任。

相關文章