lisp 利器 format 語法示例 控制字元
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Common lisp HyperSpec lisp 語法幫助Lisp
- lisp 程式碼示例Lisp
- Oracle With 語法 示例Oracle
- C# string Format示例C#ORM
- 構建利器Lisp的創作環境Lisp
- 解析程式碼的利器 Spp 語法描述語言
- YAML檔案語法及示例YAML
- CSS語法檢查利器之csslintCSS
- Lisp語言簡介Lisp
- Python語法的轉義字元Python字元
- SQL語法之SQL 萬用字元SQL字元
- SELinux策略語法以及示例策略Linux
- c#之string.format方法示例C#ORM
- GaussDB SQL基礎語法示例-迴圈語句SQL
- python爬蟲:XPath語法和使用示例Python爬蟲
- Robot Framework(8)——指令碼語法示例記錄Framework指令碼
- Java正規表示式的語法與示例Java
- Oracle 操作表結構基本語法及示例Oracle
- Lisp 永遠成不了程式設計主流語言 vs Lisp 神話之路Lisp程式設計
- 13 個示例快速入門 JS 抽象語法樹JS抽象語法樹
- 會話控制利器 gorilla/sessions會話GoSession
- 因 AI 而設計的語言 LISPAILisp
- 為什麼Lisp語言如此先進?Lisp
- alter session set events語句dump 控制檔案頭資訊示例Session
- 微信小程式WXML頁面常用語法(講解+示例)微信小程式XML
- 透過這些示例快速學習Java lambda語法Java
- ASCII 中的控制字元ASCII字元
- 為什麼我喜歡 Lisp 程式語言Lisp
- Java 語法介紹(五):條件控制(轉)Java
- 字串格式化format符號含義+轉義字元含義字串格式化ORM符號字元
- 【轉】Lisp 已死,Lisp 萬歲!Lisp
- 為什麼Lisp語言如此先進?(譯文)Lisp
- 螢幕控制示例程式
- c語言基礎語法二——迴圈控制條件語句(完節)C語言
- tar包解壓not in gzip format的解決辦法ORM
- 2.9Python基礎語法(7):轉義字元&佔位符Python字元
- newLISP 10.5.3 釋出,類 Lisp 的指令碼語言Lisp指令碼
- 轉-為什麼Lisp語言如此先進?(譯文)Lisp