在中LOOP,您可以(return)在任何表达式中使用Common Lisp表单,这将导致该LOOP表单立即求值为的值return。
LOOP也有一个return几乎几乎相同的子句,唯一的区别是您没有用括号将其括起来。该子句在LOOPDSL中使用,而形式在表达式中使用。
(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.