lisp 裡的迴圈loop macro
lisp 裡的迴圈很多種
尤其是loop 千變萬化 ,我也搞不懂,
找了一點 示例程式碼 放這裡:
CL-USER> (loop for x below 10
collect (loop for y below 10
collect (+ x y)))
((0 1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9 10) (2 3 4 5 6 7 8 9 10 11)
(3 4 5 6 7 8 9 10 11 12) (4 5 6 7 8 9 10 11 12 13) (5 6 7 8 9 10 11 12 13 14)
(6 7 8 9 10 11 12 13 14 15) (7 8 9 10 11 12 13 14 15 16)
(8 9 10 11 12 13 14 15 16 17) (9 10 11 12 13 14 15 16 17 18))
CL-USER> (loop for x below 10
for y below 10
collect (+ x y))
(0 2 4 6 8 10 12 14 16 18)
CL-USER> (loop for i
from 0
do (print i)
when (= i 5)
return 'falafel)
0
1
2
3
4
5
FALAFEL
CL-USER>
尤其是loop 千變萬化 ,我也搞不懂,
找了一點 示例程式碼 放這裡:
- Iteration for each element in a list:
> (loop for i in '(1 2 3) do (print i)) 1 2 3
- Iteration for each cdr of a list:
> (loop for i on '(1 2 3) do (print i)) (1 2 3) (2 3) (3)
- Iteration for each element in a vector:
> (loop for i across #(1 2 3) do (print i)) 1 2 3
- Iteration for each key of a hashtable:
> (setf h (make-hash-table)) > (setf (gethash 'a h) 1) > (setf (gethash 'b h) 2) > (loop for k being the hash-key of h do (print k)) b a
- Iteration for each value of a hashtable:
> (loop for v being the hash-value of h do (print v)) 2 1
- Iteration for each key-value pair of a hashtable:
> (loop for k being the hash-key using (hash-value v) of h do (format t "~a ~a~%" k v)) b 2 a 1
- i=1, while i <= 3:
> (loop for i from 1 to 3 do (print i)) 1 2 3
- i=1, while i <= 3: (different step)
> (loop for i from 1.0 to 3.0 by 0.5 do (print i)) 1.0 1.5 2.0 2.5 3.0
- i=3, while i >= 1:
> (loop for i from 3 downto 1 do (print i)) 3 2 1
- i=3, while i >= 1: (different step)
> (loop for i from 3.0 downto 1.0 by 0.5 do (print i)) 3.0 2.5 2.0 1.5 1.0
- i=1, while i < 3:
> (loop for i from 1 below 3 do (print i)) 1 2
- i=3, while i > 1:
> (loop for i from 3 above 1 do (print i)) 3 2
- Adding initialization:
> (loop with a = '(1 2 3) for i in a do (print i)) 1 2 3
- Adding subordinate variables:
> (loop for i from 1 to 3 for x = (* i i) do (print x)) 1 4 9
- Adding conditional branch for each element:
> (loop for i from 1 to 3 when (oddp i) do (print i)) 1 3
- Adding a termination condition (while):
> (loop for i from 1 to 3 while (< i 2) do (print i)) 1
- Adding a termination condition (until):
> (loop for i from 1 to 3 until (< 2 i) do (print i)) 1 2
- Collecting results as an element of a list:
> (loop for i from 1 to 3 collect (* i i)) (1 4 9)
- Concatenating results as a list:
> (loop for i from 1 to 3 append (list i i)) (1 1 2 2 3 3)
- Counting:
> (loop for i from 1 to 3 count (oddp i)) 2
- Summation:
> (loop for i from 1 to 3 sum (* i i)) 14
- Taking the maximum value:
> (loop for i from 1 to 3 maximize (mod i 3)) 2
- Taking the minimum value:
> (loop for i from 1 to 3 minimize (mod i 3)) 0
CL-USER> (loop for x below 10
collect (loop for y below 10
collect (+ x y)))
((0 1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9 10) (2 3 4 5 6 7 8 9 10 11)
(3 4 5 6 7 8 9 10 11 12) (4 5 6 7 8 9 10 11 12 13) (5 6 7 8 9 10 11 12 13 14)
(6 7 8 9 10 11 12 13 14 15) (7 8 9 10 11 12 13 14 15 16)
(8 9 10 11 12 13 14 15 16 17) (9 10 11 12 13 14 15 16 17 18))
CL-USER> (loop for x below 10
for y below 10
collect (+ x y))
(0 2 4 6 8 10 12 14 16 18)
CL-USER> (loop for i
from 0
do (print i)
when (= i 5)
return 'falafel)
0
1
2
3
4
5
FALAFEL
CL-USER>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133735/viewspace-752252/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 事件迴圈(event loop)事件OOP
- pl/sql for loop迴圈的使用SQLOOP
- JS事件迴圈Event LoopJS事件OOP
- Javascript 事件迴圈event loopJavaScript事件OOP
- JavaScript事件迴圈(Event Loop)JavaScript事件OOP
- JS 事件迴圈(Event Loop)JS事件OOP
- mysql 的procedure 中 loop迴圈的用法。MySqlOOP
- 事件迴圈(Event Loop)淺析事件OOP
- 0165-loop 迴圈OOP
- JavaScript的事件迴圈(Event loop)(附圖)JavaScript事件OOP
- 淺談js的事件迴圈(Event Loop)JS事件OOP
- nodejs中的事件迴圈 - Event LoopNodeJS事件OOP
- 瀏覽器事件迴圈Event Loop瀏覽器事件OOP
- Event loop的macro task和micro taskOOPMac
- Flutter Flame 教程2 -- Game Loop遊戲迴圈FlutterGAMOOP遊戲
- 談談 Event Loop(事件迴圈)機制OOP事件
- for 迴圈境實現遊標LOOP提取OOP
- 一文梳理JavaScript 事件迴圈(Event Loop)JavaScript事件OOP
- 瀏覽器事件迴圈機制(event loop)瀏覽器事件OOP
- JavaScript深入之事件迴圈機制(event loop)JavaScript事件OOP
- 事件迴圈Event loop到底是什麼事件OOP
- 學習筆記之事件迴圈-Event loop筆記事件OOP
- [SceneKit專題]3-Render-Loop渲染迴圈OOP
- 瀏覽器和Node不同的事件迴圈(Event Loop)瀏覽器事件OOP
- loop迴圈 長時間沒有返回結果OOP
- MYSQL儲存過程-練習4 loop迴圈MySql儲存過程OOP
- 效能優化篇 - js事件迴圈機制(event loop)優化JS事件OOP
- 為什麼要有事件迴圈機制(Event Loop)事件OOP
- nest loop內層迴圈會按照rowid排序嗎?OOP排序
- JavaScript在瀏覽器環境下的事件迴圈(Event Loop)JavaScript瀏覽器事件OOP
- Js 的事件迴圈(Event Loop)機制以及例項講解JS事件OOP
- 理解瀏覽器和nodeJs中的事件迴圈(Event Loop)瀏覽器NodeJS事件OOP
- JavaScript的map迴圈、forEach迴圈、filter迴圈、reduce迴圈、reduceRight迴圈JavaScriptFilter
- 從面試題看 JS 事件迴圈與 macro micro 任務佇列面試題JS事件Mac佇列
- JavaScript裡的迴圈方法:forEach,for-in,for-ofJavaScript
- 瀏覽器與Node的事件迴圈(Event Loop)有何區別?瀏覽器事件OOP
- 理解瀏覽器和node.js中的Event loop事件迴圈瀏覽器Node.jsOOP事件
- 筆試題——JavaScript事件迴圈機制(event loop、macrotask、microtask)筆試JavaScript事件OOPMac