3 Error Conditions
loop
is a macro, it translates the form you supply into another form. Thus, any syntactic error messages occur during macro expansion rather than during evaluation or compilation. When the Loop Facility parses the given form, it expects clauses to have loop keywords and data supplied in specific orders. Unexpected symbols or data generate an error and an error message. The message explains what the Loop Facility did not understand and where the error occurred.There are some common errors that occur when the Loop Facility is used, and many are easily corrected. This chapter discusses these errors, their causes, and possible solutions. Examples that would generate errors are also supplied.
> (loop) ; This is an infinite loop. > (loop for i from 1 count i) ; This is an infinite loop. > (loop for i = 1 then 2 ; This is a third infinite loop. while i collect i)
> (loop untl (some-condition) ; UNTIL is misspelled. do (print 'hi)) >>Error: Loop macro: Unrecognized form encountered where loop-keyword expected: (... UNTL (SOME-CONDITION) DO (PRINT (QUOTE HI)) )> (loop for i from 1 to 10 od (print 'hi)) ; DO is misspelled. >>Error: Loop macro: Unrecognized form encountered where loop-keyword expected: (... OD (PRINT (QUOTE HI)) )
> (loop for i from 1 to 10 (print 'hi)) ; A DO construct is missing. >>Error: Loop macro: Unrecognized form encountered where loop-keyword expected: (... (PRINT (QUOTE HI)) )> (loop for i in '(1 2 a 3) unless (numberp i) (return i)) ; A DO construct is missing. >>Error: Loop macro: No loop clause after conditional expression: (... UNLESS (NUMBERP I) (RETURN I)
> (loop do (print 'hi) ; AND can only be used in and ; UNLESS, IF, WHEN, and (print 'there)) ; WITH constructs. >>Error: Loop macro: A parallel AND clause is misplaced (... AND (PRINT (QUOTE THERE)))
FOR
or anAS
clause.
for
or anas
clause, the Loop Facility found a keyword that it did not expect.
> (loop for i frum 1 to 10 ; The keyword FROM is misspelled. do (print 'hi)) >>Error: Loop macro: Random atom found where FOR (or AS) preposition expected: (... FOR I FRUM TO 10 DO (PRINT (QUOTE HI)))
> (loop for i from 1 to 10 ; This is the correct form. do (print 'hi))
for
andas
constructs.
else
at the top level, it generates an error and a message.
else
is used inif
,when
,unless
, andwith
constructs. An error results whenelse
is misplaced in those constructs or when it is used inside other constructs.
> (loop else (print 'hi)) >>Error: Loop macro: Dangling ELSE clause: (... ELSE (PRINT (QUOTE HI)))
else
clause is a part of and in the correct location for the appropriate construct.
else
in conditional constructs.
DO
form.
do
, but atoms are not allowed. The Loop Facility generates an error if it finds an atomic form in ado
clause.
do
clause, you are probably trying to prevent a Compiler warning for an unused local variable. Including an atom in thedo
clause does not hide the unused variable from the Compiler; instead, it generates an error, as this example shows.
> (defun print-vars (var-list stuff info-list) (loop for var in var-list for info in info-list do stuff (format t "~S: ~S~%" var info))) >>Error: Loop macro: Atom encountered where DO forms were expected: (... do STUFF (FORMAT T "~S: ~S%" VAR INFO) )
ignore
declaration used with the Common Lisp special formdeclare
prevents a Compiler warning for the unused variablestuff
.
> (defun print-vars (var-list stuff info-list) (declare (ignore stuff)) (loop for var in var-list for info in info-list do (format t "~S: ~S~%" var info)))
do
construct.
if
orwhen
construct, it expects a conditional form, the loop keywordthen
, and a result form or forms.
then
, it generates an error and a message.
> (loop when (true-p)) ; This clause is incomplete. >>Error: Loop macro: No loop clause after conditional expression: (... WHEN (TRUE-P)
if
andwhen
constructs have an appropriate resulting clause beginning withthen
.
> (loop when (true-p) do (print 'hi)) ; This version is correct.
if
andwhen
.
FOR
,AS
, orREPEAT
iteration construct follows loop body code.
do
,thereis
,always
, and all accumulating clauses must followfor
,as
, andrepeat
clauses.
for
clause should appear before thedo
clause; it should never follow ado
clause.
> (loop do (print 'hi) for i from 1 to 10) >>Error: Loop macro: FOR iteration must not follow body code: (... (FOR I FROM 1) )
> (loop for i from 1 to 10 do (print 'hi))
FOR
,AS
, orREPEAT
iteration starts inside of a conditional construct.
for
,as
, orrepeat
clause must be at the top level inside a loop. Though these clauses can be part of a conditional statement, they cannot be used insidewhen
,if
, orunless
clauses.
for
clause is used inside of awhen
clause, and an error is signaled.
> (loop for x in mylist when x as y = (+ x 3) collect y) >>Error: Loop macro: FOR iteration must not start inside of conditional: (... (AS Y = (+ X 3)) COLLECT Y )
> (setq mylist '(3 7 10 13 15 18 21)) (3 7 10 13 15 18 21) > (loop for x in mylist as y = (and x (+ x 3)) when x collect y) (6 10 13 16 18 21 24)
> (loop for i from 1 for i from 10) >>Error: Loop macro: Duplicated variable: I
> (loop with i for i from 1 to 10 collect a into i) >>Error: Loop macro: Duplicated variable: I
> (loop collect i into i) >>Error: Loop macro: 'collection' accumulate form is the same as the collection variable: I
> (loop collect i into j)
thing
is not defined.
> (loop for i being each thing in 'something) >> Loop macro: Loop method not found: (... I BEING EACH THING IN (QUOTE SOMETHING) )
some-var
to an existing numeric value. Butsome-var
is not a number, so Lisp generates this error and a message.
> (loop for i in '(1 2 3 some-var) sum i) >>Error: The value of VAR, SOME-VAR, should be a NUMBER
for
andas
constructs; Section 2.4 on page 24 for more information about constructs that accumulate numeric values.
for
keyword should be followed by a variable, but2
is not a valid variable name.
> (loop for 2 to 10 do (print i)) >>Error: Loop macro: Encountered a random constant, 2, where a variable name is expected: (... FOR (TO 10 DO (PRINT I))
for
and restructure the clause appropriately.
> (loop for i from 2 to 10 do (print i))
> (loop with (a b c) = 5 return 'done) >>Error: Loop macro: Destructuring form (A B C) isn't congruent to constant value 5
with
variables as specified.
> (loop with (a b a) do (print a)) >>Error: Loop macro: Duplicated variable: A
define-loop-sequence-path
anddefine-loop-path
and the methodsinterned-symbols
andlocal-interned-symbols
are obsolete in the Loop Facility.
Generated with Harlequin WebMaker