common lisp 学习第四天 变量、宏
生活随笔
收集整理的這篇文章主要介紹了
common lisp 学习第四天 变量、宏
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//變量 //引入變量:變量作用域括號(hào)內(nèi) (let ((x 10) (y 20) z) ...) 引入變量列表中的變量 (let* ((x 10)? (y (+ x 10)) ...) //全局變量 之前有沒(méi)有值都可以賦值 (defparameter *count*變量名0值"doc描述") 變量未定以才可以賦值,也可以不給定值 (defvar *count*變量名?0值?"doc描述") //常量 (defconstant a常量名10值) //賦值 (setf place保存值的位置?value值) 遞增 (incf x)——(setf x (+ x 1)) (incf x 10)——(setf x (+ x 10)) 遞減 (decf x)——(setf x (- x 1)) 位置間輪換值 (rotatef a b) 向左移動(dòng)賦值a=b,b=20 (shiftf a b 20)
//宏 //if (ifcondition條件?then-form滿(mǎn)足條件執(zhí)行[else-form]不滿(mǎn)足條件執(zhí)行) //when當(dāng)滿(mǎn)足條件執(zhí)行,unless當(dāng)不滿(mǎn)足條件執(zhí)行 (whencondition條件 執(zhí)行1 執(zhí)行2) 定義宏 (defmacro my-when (condition &rest body) `(if ,condition (progn ,@body))) (defmacro my-unless (condition &rest body) `(if (not ,condition) (progn ,@body))) progn操作符:逐個(gè)執(zhí)行后面的語(yǔ)句。 //cond多重分支 (cond (test-1條件form*執(zhí)行體) ... (test-N form*)) 可以用t代表if中的else分支 (cond (a (do-x)) (b (do-y)) (t (do-z))) //and or是宏,not是函數(shù) //dolist循環(huán)列表操作 (dolist (var存儲(chǔ)每個(gè)值的變量?list-form列表) body-form*循環(huán)體) 循環(huán)打印 (dolist (x '(1 2 3)) (print x)) 列表結(jié)束前中斷,用return (dolist (x '(1 2 3)) (print x) (if (evenp x) (return))) //dotimes循環(huán)計(jì)數(shù),用return中斷 (dotimes (var變量?count-form循環(huán)次數(shù)) body-form*循環(huán)體) CL-USER> (dotimes (i 2) (print i)) 0? 1? NIL //do循環(huán)允許定義任意數(shù)量變量,變量改變方式可控,可以定義終止條件。 變量列表也可為空,但是必須保留括號(hào)。 (do (variable-definition*變量定義) (end-test-form結(jié)束判斷 result-form*返回結(jié)果) statement*) variable-definition變量定義形式 (var變量名 init-form初始值 step-form步長(zhǎng)) CL-USER> (do ((i 0 (1+ i))) ((>= i 2)) (print i)) 0? 1? NIL 其中(1+ n)等同于(+ 1 n) //loop 簡(jiǎn)單形式:無(wú)限循環(huán),通過(guò)return結(jié)束 (loop body-form*循環(huán)體) 擴(kuò)展形式 構(gòu)建列表 CL-USER> (loop for i from 1 to 10 collecting i) (1 2 3 4 5 6 7 8 9 10) 計(jì)算平方數(shù)求和 CL-USER> (loop for x from 1 to 10 summing (expt x 2)) 385 統(tǒng)計(jì)字符串中元音字母?jìng)€(gè)數(shù) CL-USER> (loop for x across "the quitck brown fox jumps over the lazy dog" counting (find x "aeiou")) 11 計(jì)算第11個(gè)斐波那契數(shù) CL-USER> (loop for i below 10 and a = 0 then b and b = 1 then (+ b a) finally (return a)) 55
//宏 //if (ifcondition條件?then-form滿(mǎn)足條件執(zhí)行[else-form]不滿(mǎn)足條件執(zhí)行) //when當(dāng)滿(mǎn)足條件執(zhí)行,unless當(dāng)不滿(mǎn)足條件執(zhí)行 (whencondition條件 執(zhí)行1 執(zhí)行2) 定義宏 (defmacro my-when (condition &rest body) `(if ,condition (progn ,@body))) (defmacro my-unless (condition &rest body) `(if (not ,condition) (progn ,@body))) progn操作符:逐個(gè)執(zhí)行后面的語(yǔ)句。 //cond多重分支 (cond (test-1條件form*執(zhí)行體) ... (test-N form*)) 可以用t代表if中的else分支 (cond (a (do-x)) (b (do-y)) (t (do-z))) //and or是宏,not是函數(shù) //dolist循環(huán)列表操作 (dolist (var存儲(chǔ)每個(gè)值的變量?list-form列表) body-form*循環(huán)體) 循環(huán)打印 (dolist (x '(1 2 3)) (print x)) 列表結(jié)束前中斷,用return (dolist (x '(1 2 3)) (print x) (if (evenp x) (return))) //dotimes循環(huán)計(jì)數(shù),用return中斷 (dotimes (var變量?count-form循環(huán)次數(shù)) body-form*循環(huán)體) CL-USER> (dotimes (i 2) (print i)) 0? 1? NIL //do循環(huán)允許定義任意數(shù)量變量,變量改變方式可控,可以定義終止條件。 變量列表也可為空,但是必須保留括號(hào)。 (do (variable-definition*變量定義) (end-test-form結(jié)束判斷 result-form*返回結(jié)果) statement*) variable-definition變量定義形式 (var變量名 init-form初始值 step-form步長(zhǎng)) CL-USER> (do ((i 0 (1+ i))) ((>= i 2)) (print i)) 0? 1? NIL 其中(1+ n)等同于(+ 1 n) //loop 簡(jiǎn)單形式:無(wú)限循環(huán),通過(guò)return結(jié)束 (loop body-form*循環(huán)體) 擴(kuò)展形式 構(gòu)建列表 CL-USER> (loop for i from 1 to 10 collecting i) (1 2 3 4 5 6 7 8 9 10) 計(jì)算平方數(shù)求和 CL-USER> (loop for x from 1 to 10 summing (expt x 2)) 385 統(tǒng)計(jì)字符串中元音字母?jìng)€(gè)數(shù) CL-USER> (loop for x across "the quitck brown fox jumps over the lazy dog" counting (find x "aeiou")) 11 計(jì)算第11個(gè)斐波那契數(shù) CL-USER> (loop for i below 10 and a = 0 then b and b = 1 then (+ b a) finally (return a)) 55
總結(jié)
以上是生活随笔為你收集整理的common lisp 学习第四天 变量、宏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ECMA6--字符串/数组
- 下一篇: wireshark抓包分析怎么看进程_w