From: Johann Hibschman
Subject: Controlling indents in deep-nested fcns?
Date: 
Message-ID: <5r0cfn$3re@agate.berkeley.edu>
Hi all,

I've been finding myself indented all the way over to the right side
of the screen while writing some code, and I was wondering if anyone
has any suggestions for controlling "creeping whitespace" while
still maintaining readable code.

Usually, I try to break things up into small enough functions that I
don't run into problems, but I can't seem to worm my way out of this
one.

Briefly, I am implementing a mathematical-style functions which depends
on a set of constants, a location (radius), and the actual function
parameters.  I would like to be able to set the constants and the
radius and precompute some numeric coefficients based on that
information, so I can quickly compute the value based on the real
parameters.  Since that's hopelessly abstract, a code sketch is:

(defun f ()
  (let ((g (generate-g-approximation-function)))
    #'(lambda (const)
        (let ((c1 (* (const-a const) (const-b const))))
          #'(lambda (r)
              (let ((c2 (* r (const-c const)))))
                #'(lambda (x)
                    (if (> x 0)
                        (+ (funcall g (* c2 x)) c1)
                        12)))))))

Does anyone have any good ideas of ways around this?  Or a better
approach that avoids the problem?  Standard currying doesn't let
me precompute any coefficients.

Basically, I'm going to end up integrating over x, so I want
computations of the interior lambda to be as fast as possible.


- Johann

-- 
Johann A. Hibschman         | Grad student in Physics, working in Astronomy.
······@physics.berkeley.edu | Probing pulsar pair production processes.

From: William Paul Vrotney
Subject: Re: Controlling indents in deep-nested fcns?
Date: 
Message-ID: <vrotneyEDp4GJ.Gy6@netcom.com>
In article <··········@agate.berkeley.edu> ······@physics12.Berkeley.EDU
(Johann Hibschman) writes:

>
>   I've been finding myself indented all the way over to the right side
>   of the screen while writing some code, and I was wondering if anyone
>   has any suggestions for controlling "creeping whitespace" while
>   still maintaining readable code.
>
>   Usually, I try to break things up into small enough functions that I
>   don't run into problems, but I can't seem to worm my way out of this
>   one.
>
>   Briefly, I am implementing a mathematical-style functions which depends
>   on a set of constants, a location (radius), and the actual function
>   parameters.  I would like to be able to set the constants and the
>   radius and precompute some numeric coefficients based on that
>   information, so I can quickly compute the value based on the real
>   parameters.  Since that's hopelessly abstract, a code sketch is:
>
>   (defun f ()
>     (let ((g (generate-g-approximation-function)))
>       #'(lambda (const)
>           (let ((c1 (* (const-a const) (const-b const))))
>             #'(lambda (r)
>                 (let ((c2 (* r (const-c const)))))
>                   #'(lambda (x)
>                       (if (> x 0)
>                           (+ (funcall g (* c2 x)) c1)
>                           12)))))))
>
>   Does anyone have any good ideas of ways around this?  Or a better
>   approach that avoids the problem?  Standard currying doesn't let
>   me precompute any coefficients.
>

If I had the bigger picture of your problem I might suggest that you don't
need to use closures in the way you are using them to solve it elegantly and
precompute the coefficients.  In the absence of that, to avoid the creeping,
how about something (untested) like

    (defun f ()
      (let (c1 c2 const1)
        (labels 
         ((faa (x)
               (if (> x 0)
                   (+ (funcall g (* c2 x)) c1)
                 12))
          (fab (r)
               (setq c2 (* r (const-c const1)))
               (function faa))
          (fac (const)
               (setq const1 const
                     c1 (* (const-a const) (const-b const)))
               (function fab)))
         (function fac))))


-- 

William P. Vrotney - ·······@netcom.com
From: Emergent Technologies Inc.
Subject: Re: Controlling indents in deep-nested fcns?
Date: 
Message-ID: <5r275a$6le$1@newsie.cent.net>
 >In article <··········@agate.berkeley.edu> ······@physics12.Berkeley.EDU
>(Johann Hibschman) writes:
>
>>
>>   I've been finding myself indented all the way over to the right side
>>   of the screen while writing some code, and I was wondering if anyone
>>   has any suggestions for controlling "creeping whitespace" while
>>   still maintaining readable code.

>>   (defun f ()
>>     (let ((g (generate-g-approximation-function)))
>>       #'(lambda (const)
>>           (let ((c1 (* (const-a const) (const-b const))))
>>             #'(lambda (r)
>>                 (let ((c2 (* r (const-c const)))))
>>                   #'(lambda (x)
>>                       (if (> x 0)
>>                           (+ (funcall g (* c2 x)) c1)
>>                           12)))))))
>>
>>   Does anyone have any good ideas of ways around this?  Or a better
>>   approach that avoids the problem?  Standard currying doesn't let
>>   me precompute any coefficients.
>>

There's no law that says you have to indent according to convention.
While I wouldn't advocate a cavalier additude toward indentation,
this looks like a perfect place to invent an indentation convention
that addresses this particular coding.
  I think modifying your
approach to fit the domain is always a good idea, but modifying
your approach solely to avoid ``creeping'' in the text may not be. 
From: Johann Hibschman
Subject: Re: Controlling indents in deep-nested fcns?
Date: 
Message-ID: <5r2uok$3uo@agate.berkeley.edu>
In article <············@newsie.cent.net>,
Emergent Technologies Inc. <········@eval-apply.com> wrote:

>>>   (defun f ()
>>>     (let ((g (generate-g-approximation-function)))
>>>       #'(lambda (const)
>>>           (let ((c1 (* (const-a const) (const-b const))))
>>>             #'(lambda (r)
>>>                 (let ((c2 (* r (const-c const)))))
>>>                   #'(lambda (x)
>>>                       (if (> x 0)
>>>                           (+ (funcall g (* c2 x)) c1)
>>>                           12)))))))
>>>
>>>   Does anyone have any good ideas of ways around this?  Or a better
>>>   approach that avoids the problem?  Standard currying doesn't let
>>>   me precompute any coefficients.
>>>
>
>There's no law that says you have to indent according to convention.
>While I wouldn't advocate a cavalier additude toward indentation,
>this looks like a perfect place to invent an indentation convention
>that addresses this particular coding.
>  I think modifying your
>approach to fit the domain is always a good idea, but modifying
>your approach solely to avoid ``creeping'' in the text may not be. 

I had another screwball idea.  If CL were still dynamically scoped,
I could write:

(defun f1 (const)
  (let ((c1 (some-operation-on const)))
    #'(lambda (thunk)
        (funcall thunk))))

(defun f2 (x)
  #'(lambda ()
      (* x c1)))

And call it with
(let ((const-part (f1 const)))
  (funcall const-part (f2 x)))

with the idea being that when the c1 in the lambda in f2 is looked
up inside f1 that it will see the c1 in the enclosing frame rather
than going directly up to the global scope.  (I'm more familiar
with the scoping behavior of Scheme; tell me if I'm talking about
things wrongly for CL.)  (Wrongly?  Whatever.)

Clearly this doesn't work.  However, is there any way I could wrap
up a frame of bindings (my CS vocabulary fails me now) and pass
it to another function, so that function could look up variables
in that frame, if it so chose?

i.e. (in pseudocode)
(defun f1 (const)
  (let ((c1 (some-operation-on const)))
    (wrap-up-the-current-bindings)))

(defun f2 (thingy x)
   (* x (lookup-in-frame thingy c1)))

Called with (f2 (f1 const) x).

(BtW, I don't want to pass around explicit lists of values, since I
 would like to be able to somehow compose two functions like this
 eventually, so I want to keep it as general as possible.)

- Johann

-- 
Johann A. Hibschman         | Grad student in Physics, working in Astronomy.
······@physics.berkeley.edu | Probing pulsar pair production processes.
From: Harley Davis
Subject: Re: Controlling indents in deep-nested fcns?
Date: 
Message-ID: <33D55B59.61CE73EE@ilog.com>
Johann Hibschman wrote:

> Hi all,
>
> I've been finding myself indented all the way over to the right side
> of the screen while writing some code, and I was wondering if anyone
> has any suggestions for controlling "creeping whitespace" while
> still maintaining readable code.
>
> Usually, I try to break things up into small enough functions that I
> don't run into problems, but I can't seem to worm my way out of this
> one.
>
> (defun f ()
>   (let ((g (generate-g-approximation-function)))
>     #'(lambda (const)
>         (let ((c1 (* (const-a const) (const-b const))))
>           #'(lambda (r)
>               (let ((c2 (* r (const-c const)))))
>                 #'(lambda (x)
>                     (if (> x 0)
>                         (+ (funcall g (* c2 x)) c1)
>                         12)))))))
>
> Does anyone have any good ideas of ways around this?  Or a better
> approach that avoids the problem?  Standard currying doesn't let
> me precompute any coefficients.

How about this:

(defun make-f1 (g c1 c2)
   #'(lambda (x)
       (if (> x 0)
           (+ (funcall g (* c2 x)) c1)
           12)))

(defun make-f2 (c1 g)
  #'(lambda (r)
       (let ((c2 (* r (const-c const))))
          (make-f1 g c1 c2))))

(defun make-f3 (g)
   #'(lambda (const)
        (let ((c1 (* (const-a const) (const-b const))))
           (make-f2 c1 g))))

(defun f ()
   (let ((g (generate-g-approximation-function)))
     (make-f3 g)))

It's exactly the same but avoids any creepiness.

-- Harley