RETURN 子句與 RETURN 表單
在 LOOP
中,你可以在任何表示式中使用 Common Lisp (return)
表單,這將使 LOOP
表單立即計算為 return
的值。
LOOP
也有一個 return
子句幾乎完全相同,唯一的區別是你不用括號括起來。該子句在 LOOP
的 DSL 中使用,而表單在表示式中使用。
(loop for x in list
do (if (listp x) ;; Non-barewords after DO are expressions
(return :x-has-a-list)))
;; Here, both the IF and the RETURN are clauses
(loop for x in list
if (listp x) return :x-has-a-list)
;; Evaluate the RETURN expression and assign it to X...
;; except RETURN jumps out of the loop before the assignment
;; happens.
(loop for x = (return :nothing-else-happens)
do (print :this-doesnt-print))
finally
之後的東西必須是一個表示式,所以必須使用 (return)
形式而不是 return
子句:
(loop for n from 1 to 100
when (evenp n) collect n into evens
else collect n into odds
finally return (values evens odds)) ;; ERROR!
(loop for n from 1 to 100
when (evenp n) collect n into evens
else collect n into odds
finally (return (values evens odds))) ;; Correct usage.