[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Trailing parens

Paul Prescod wrote:
> IMO, human beings just prefer flatness to hierarchy and iteration to
> recursion. I know that when I write Scheme (or really any language) I
> often consciously flatten my code by defining local variables or
> creating functions.

The flatness preference is easily explained: when a hierarchy doesn't
provide any benefit, flatness is preferable, because it's simpler by any
measure.  The fact that in some mathematical sense, a series of dependent
definitions can "naturally" be structured as a hierarchy, doesn't mean that
humans must or should program that way, if a simpler linear sequence can
express the same meaning.

On recursion vs. iteration, though, I have to disagree.  If you think
recursion is unnatural, you may be looking at it the wrong way.  Think of it
as a goto (sometimes with parameters).  At the end of a block, when you're
ready to loop around to the beginning, you just specify the name of the
"label" (function) which you want to go to.  At the source code level,
there's no difficulty here, and it's easily taught to someone without prior
expectations.  I believe the biggest problem that most programmers face when
learning about recursion is their mental model of what must be happening
under the hood, based on their understanding of strictly stack-based
languages in which a function call always pushes the stack.  Once freed from
that piece of baggage, recursion becomes very natural.

BTW, there is an additional piece of housekeeping when using recursion, i.e.
specifying the parameters in the recursive call.  But as always, this is a
tradeoff, and recursive languages are hardly unique in having occasional
housekeeping requirements.

Finally, because of the close correspondence between recursion and
iteration, it is trivial for a functional language to provide traditional
iterative constructs.  The reverse, for imperative languages, is not usually
the case.