[plt-scheme] Paren Paralysis Sufferers Unite

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Fri Oct 16 16:40:09 EDT 2009

A word of caution: to an experienced Scheme/Lisp programmer, that code
is pretty unreadable.  I'm very used to using the indentation of code,
and the auto-selecting of open/close pairs, to read my code, and not
having lots of blank lines with a single parenthesis in them to
intervene.  It's about the equivalent of writing the following in C
(or C++, Java, Perl, any of those language):

((((2 * x) * y) + (((y * x) / z) / 7)) - (((a * b) / c) / d))

Sure, the extra parentheses make it clear what goes with what, but you
can just write:

2*x*y + y*x/z - a*b/c/d

or, for a little more caution,

(2*x*y) + (y*x/z) - (a*b/c/d)

...to get the same thing.  Putting in lots of extra parentheses (or
newlines, in the Scheme case) can clutter things up, instead of clear
them up, when you get comfortable with a language.  Use them now if
you need to, to get started, but if you're going to use Scheme
long-term you might want to get comfortable with closing all your
parentheses on one line.

Carl Eastlund

On Fri, Oct 16, 2009 at 4:31 PM, Christopher Coleman
<mellowfish at gmail.com> wrote:
> These tricks are great, I use them all the time in my coding even
> though I've been at it for a while now.  Its just so much easier to
> think line by line instead of trying to keep track of which set of
> parens I am in on a single line.
>
> Also, as a tutor I teach my students the same concept of paren pairing
> and filling with {} in c-style languages.
>
> On Fri, Oct 16, 2009 at 1:04 PM, David Storrs <david.storrs at gmail.com> wrote:
>> On Fri, Oct 16, 2009 at 3:55 PM, Carl Eastlund <carl.eastlund at gmail.com> wrote:
>>
>>> That, and use a Scheme-conscious editor like DrScheme (or Emacs or vi,
>>> if you're familiar).
>>
>> Carl beat me to it on this one.
>>
>> Two more tricks you can do to simplify your life:
>>
>> 1) Always write parens in pairs, then fill in what goes inside them.
>>
>> 2) Add newlines to make it clearer what goes with what.  This isn't
>> the "done thing" in Scheme, but it's a useful tool while you're
>> learning.  So:
>>
>> (define (foo a b (c 7))
>>  (let (
>>          (x a)
>>        )
>>    (begin
>>      (display a b c)
>>     )
>> )) ; let and define
>>
>> A decent editor will handle the alignment for you.
>>
>> --Dks


Posted on the users mailing list.