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.