Re: anamorphic macros



Tron3k wrote:
Hi, I'm wondering why Loop is unfit for use in macros. If so, I might
have some troubles. =)

I think Frode may be prejusiced against loop. Acceptance of loop is something of a religious issue among Lisp programmers. It is counterproductive to argue about it, but I'll go just this far: It is certainly possible that a buggy loop implementation could expand into code that has pathological anamorphicisms, but I see no way in which these problems would be fundamental to the semantics of loop. Rather, they would be considered bugs.

The closest thing I've seen to a common anamorphicism problem in
loop is that loop expands into a block that by default is names
nil.  Nil is a symbol exported from the cl package, and therefore
use of loop inside the expansion of some iteration macro may
promiscuously block [sic -- a pun!] some inner return-from.  A
quick example (untested):

(defmacro dolist-by-cddr ((var list &optional retform) &body body)
  (let ((x (gensym))
    `(loop for x on ,list by #'cddr
        as ,var = (car ,x)
        do ,@body
        finally (return ,retform))))

Now, if someone used this macro this way

  (dolist (x my-caches)
    (dolist-by-cdr (y x)
      ...
      (when mumbl (return nil))
      ...))

the return-from would incorrectly return from the nil-named block in
the loop inserted by dolist-by-cddr.

The fix for this is either to document clearly that dolist-by-cddr
inserts a nil-named block (lisk the other standard do-mumbl macros)
or else to add a
  named ,(gensym)
clause to the dolist-by-cddr expansion.  More oftent than not
programmers forget to do one or the other.  When they do, and when
a user programmer happens to use a return in the body, it can be
a difficult bug to diagnose.
.



Relevant Pages

  • Re: Trying to use STRING
    ... "A loop-with-exit loop is a loop in which the exit condition ... and Style Guidlines for Professional Programmers." ... While the construct didn't make it into the COBOL ... was apparently a subscript (but not formatted as a refmodded subscript ...
    (comp.lang.cobol)
  • Re: [EGN] Variable hoisting
    ... As Randy has already pointed out, ... > lines of compiler code and a book and despite the fact that despite ... Incompetent programmers are hardly a rarity. ... you rely on the compiler to extricate your loop invariants - things like ...
    (comp.programming)
  • Re: Adding a Par construct to Python?
    ... > professional programmers to grasp. ... For 'amateur' programmers we need to ... that *any* for loop could be converted to a parallel one. ... dependencies before swapping 'par' for 'for', ...
    (comp.lang.python)
  • Re: Smart programming languages
    ... > People often confuse pragmatism based upon experience with cynicism. ... Should programmers require a legally binding license to ... It could also unroll the loop ... > Randy Howard ...
    (comp.programming)
  • Re: Use of "return" in place of "last" (newbie question)?
    ... out of a foreach loop with return. ... functional programmers it is a sacrilege. ... more elegant alternative which would satisfy the functional programmers? ... to analyse where which flag is being set under what conditions. ...
    (comp.lang.perl.misc)