Is there a style convention for Common Lisp recursive helper functions? -


i know if there style guideline, published ansi or implementation authors or influential authority, lisp functions implemented using recursive helper functions take additional parameters person calling function doesn't need think about. here simplest example can think of. of these three, if any, preferred in standardized style guide common lisp (if there one)?

(defun factorial (n)    (defun tail-factorial (n m)       (if (= n 1)           m           (tail-factorial (- n 1) (* n m))))    (tail-factorial n 1)) 

this seems unpleasant because of function declaration within function declaration. use lambda avoid naming helper function , convoluting things, not clear how recurse within lambda expression having call itself. if there way have anonymous function call itself, seems messy, if helper needs helper needs helper ...

another alternative declare tail guy first (or after, makes sbcl complain):

(defun tail-factorial (n m)    (if (= n 1)       n       (tail-factorial (- n 1) (* n m)))) (defun factorial (n)    (tail-factorial n 1)) 

this seems unpleasant because reading code see tail-factorial , might feel need understand it, though it's helper function factorial , won't used elsewhere. in own code, keep writing functions analogous , struggle come comments make me re-understand did months or years now. again, situation gets bad here when helper needs helper needs ...

another alternative uses optionals:

(defun factorial (n &optional (m 1))    (if (= n 1)       m       (factorial (- n 1) (* n m)))) 

this seems unpleasant because expect 1 argument factorial. nobody outside function have reason call second argument. find annoying try understand code optional arguments never use.

now, i'm clear asking think best kind of subjective conversation stackoverflow dislikes, objective question whether or not there kind of standardized style guideline of these alternatives preferred. use sbcl , have not found guideline sort of thing on site, , not aware of such guide published ansi or other standardizing body, including other implementors.

perhaps there alternative altogether, , welcome suggestions. keep running situation of needing little helper function (who needs helper function, etc) , want used writing in way people find clear. asked similar question @ recursing in lambda function , , several people recommended favorite pet projects nobody mentioned style guideline.

thanks in advance!

defun makes global functions. if use defun inside defun making new global function @ each time call it. make local lexical function used inside factorial standard way labels

(defun factorial (n)    (labels ((tail-factorial (n m)               (if (= n 1)                   m                   (tail-factorial (- n 1) (* n m)))))      (tail-factorial n 1))) 

common lisp has no guarantee tail call optimization might blow stack. serious work have used loop macro this.

optional arguments works, shouldn't make internals optional if it's not intention user ever want supply it. it's leaking abstraction , making confusing documentation.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -