guile-user
[Top][All Lists]
Advanced

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

Re: On procedures within procedures


From: Ian Price
Subject: Re: On procedures within procedures
Date: Wed, 03 Apr 2013 16:46:06 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

Mike Gran <address@hidden> writes:

> I've been looking over one of the solutions to a Guile 100 problem
> (a basic version of the `ls' command) at the link below.  It is
> interesting stylistically because it works very hard at minimizing the
> scope of procedures.  It uses a lot of procedures within procedures,
> like, for example, within a `let*' block.
I think I first heard of people doing this in one of the "letrec
reloaded" papers. It confused me then; it confuses me now :).

> Personally, I only define a procedure within a procedure if the
> inner procedure is not a pure function, e.g. if its result
> depends on information other than its parameters, and that
> information only exists within the scope of the outer procedure.
> I tend to keep most pure functions at the module level. But,
> I'm not a very Schemey guy.
Well, you can get around this by passing in the free variables if you
really want to put everything at the top level (some functional
compilers do this, and it is called lambda-lifting).

> One could look at using lots of procedures within procedures as good
> defensive programming style: hiding unnecessary functions.
>
> Or one could look at it as being less clear because you end up with
> longer "paragraphs" to read.
This is how I'd view it: it's cute, but unless you are using a Scheme
implementation without a module system (i.e. a bad one), I don't think
it's going to do you much good in the long run.

> Do you have a personal philosophy on how much you try to minimize
> the scope of procedures?  If you were writing a tutorial,
> what would you say?
For me it boils down to the question of "will I use this in another
procedure"? If no, keep it as an inner define; if yes, put it in the top
level.

Obviously, I'm not a clairvoyant, so this is usually a guess, but
experience gives you a "feel" for it, and you can revise later.

_Sometimes_ I do it because I know Guile can inline it if it is an inner
define (naughty, naughty).

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]