Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

TAILPROG

Name: Anonymous 2010-01-31 11:01


(defmacro tailprog (let-bindings pseudo-funcs &rest forms)
  (let (argtags-forms macrolet-elems)
    (dolist (pfunc pseudo-funcs)
      (destructuring-bind (name vars &rest forms) pfunc
        (push `(label ,name ,@vars) argtags-forms)
        (push `(return (progn ,@forms)) argtags-forms)
        (push `(,name (&rest args) `(goto ,',name ,@args)) macrolet-elems)))
    `(macrolet (,.(nreverse macrolet-elems))
       (let ,let-bindings
         (argtags nil
           (return (progn ,@forms))
           ,.(nreverse argtags-forms))))))

ALL YOUR TAIL RECURSION R BELONG TO US

Name: Anonymous 2010-02-01 14:13

>>11
LOOP, like all other macros, eventually expand to s-expressions which contain nothing but function calls and special operators. LOOP is nothing more than a simple domain-specific language for purposes of iteration which is compiled to CL. Doing TCO in CL isn't that terribly hard, eventually everything gets compiled down to simple special forms, and then possibly to some even more low-level IR language, where one can perform TCO not that much differently from how you can do it in Scheme, however there are certain language features that can lead to difficulty in performing such optimizations, such as CL's optional dynamic scope (you can declare variables as being ``special'', either locally, or globally. If a name is special within some environment(if local, then there's a corresponding declaration, if global, then the name was proclaimed special(within some package)), then the dynamic binding of that name is accessed). This usually requires a stack to be used to implement dynamic binding, however there are some tricks that allow TCO to be performed in some cases. I've just tested if TCO was performed by a certain decent CL implementation, in one case of dynamic rebinding in a tail-recursive function, and it was not(even at [m](optimize (debug 0) (safety 0) (speed 3))[m/] settings), however in the case of lexical scope, it perfomed quite nicely.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List