In
Common Lisp two ways of defining labels exist. The first one involves the tagbody special operator. Distinguishing its usage from many other programming languages that permit global navigation, such as
C, the labels are only accessible in the context of this operator. Inside of a tagbody labels are defined as forms starting with a symbol; the go special form permits a transfer of control between these labels. (let ((iteration NIL)) (tagbody start (print 'started) (setf iteration 0) increase (print iteration) (incf iteration 1) (go check) check (if (>= iteration 10) (go end) (go increase)) end (print 'done))) A second method utilizes the reader macros #
n= and #
n#, the former of which labels the object immediately following it, the latter refers to its evaluated value. Labels in this sense constitute rather an alternative to variables, with #
n= declaring and initializing a “variable” and #
n# accessing it. The placeholder
n designates a chosen unsigned decimal integer identifying the label. (progn #1="hello" (print #1#)) Apart from that, some forms permit or mandate the declaration of a label for later referral, including the special form block which prescribes a naming, and the loop macro that can be identified by a named clause. Immediate departure from a named form is possible by using the return-from special operator. (block myblock (loop for iteration from 0 do (if (>= iteration 10) (return-from myblock 'done) (print iteration)))) (loop named myloop for iteration from 0 do (if (>= iteration 10) (return-from myloop 'done) (print iteration))) In a fashion similar to C, the macros case, ccase, ecase, typecase, ctypecase and etypecase define switch statements. (let ((my-value 5)) (case my-value (1 (print "one")) (2 (print "two")) ((3 4 5) (print "three four or five")) (otherwise (print "any other value")))) (let ((my-value 5)) (typecase my-value (list (print "a list")) (string (print "a string")) (number (print "a number")) (otherwise (print "any other type")))) ==See also==