emacs-devel
[Top][All Lists]
Advanced

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

Re: Lexical let and setq


From: Pascal J. Bourguignon
Subject: Re: Lexical let and setq
Date: Sat, 14 Sep 2013 16:04:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

Lars Magne Ingebrigtsen <address@hidden> writes:

> Stefan Monnier <address@hidden> writes:
>
>> That one is OK, since recursion is not supported efficiently.
>> The problem is when people use the above because they're writing (poor)
>> C code in Elisp (e.g. they begin their functions with a big let
>> declaring all the local vars that they may use later on in the
>> function).
>
> I think the most common reason for stashing a lot of variables in a let
> is to avoid infinite indentation.
>
> (let ((a (foo)))
>   (something)
>   (let ((b (something-else)))
>     (more a b)
>     (let ((c (yet-more)))
>       (zot a b c))))
>
> vs
>
> (let ((a (foo))
>       b c)
>   (something)
>   (setq b (something-else))
>   (more a b)
>   (setq c (yet-more))
>   (zot a b c))
>
> I kinda think the latter form is sometimes more readable.  

Err, no.  A form with less indentation MAY be more readable, but not
when it's a procedural form with a setq every other subform.

If something similar and systematicall occurs from one level of
indentation to the other, then you can write a macro that will do this
uniform something, and have a unindented body:

   (something-similar 
       (foo)
       (something)
       (something-else)
       (more)
       (yet-more)
       (zot))


A simple example would be:

  (defmacro pipe (input &rest functions)
    (pipe-aux input functions))
 
  (defun pipe-aux (input functions)
    (if functions
        (pipe-aux (list (first functions) input)
                  (rest functions))
        input))
 
  (macroexpand '(pipe foo a b c d e f g)) ; --> (g (f (e (d (c (b (a foo)))))))

So you just write

  (pipe foo
     a
     b
     c
     d
     e
     f
     g)

or rather:

  (pipe foo a b c d e f g)



But in case of your let, it is much harder to read, because now you must
rebuild the data flow from the variables and setq expressions.  The
indented form was much clearer, because you can more easily skip a
subexpression, knowing for sure that whatever happens to variables in
subexpressions can't affect the rest.



-- 
__Pascal Bourguignon__
http://www.informatimago.com/




reply via email to

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