all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Moving between headers in message-mode
Date: Sat, 15 Jun 2013 20:22:03 +0200	[thread overview]
Message-ID: <87hagzfd4k.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 87r4g3mhnz.fsf@VLAN-3434.student.uu.se

Emanuel Berg <embe8573@student.uu.se> writes:

> "Pascal J. Bourguignon" <pjb@informatimago.com> writes:
>
> First, thanks *a lot*, however, your feedback is above me, I must
> admit. Perhaps, you can elaborate the four points below a bit? Or
> perhaps point me to related material?
>
>> I would factorize out the body ...

This is what I've done in the code I included in my message.  Now, at
first the local function was named body, but then it's good also to try
to give more meaningful names to functions, even if they are local
functions.

Now, the first thing is that in simple cases, you could just use let:


Instead of:

    (let (color)
      (if prefixp
          (setf color "red")
          (setf color "blue"))
      (set-foreground-color color)
      (set-cursor-color color))

write:
    
    (let ((color (if prefixp "red" "blue")))
      (set-foreground-color color)
      (set-cursor-color color))

but in your case, you have a lot of variables that depend on the same
condition, so it would be awkward to repeat it in all initialization
expressions.


Now one thing is that let is equivalent to an anonymous function call
(see: http://home.pipeline.com/~hbaker1/MetaCircular.html )


    (let ((a e1)
          (b e2)
          (c e3))
       (do-something-with a b c)
       (and-something-with b c))

<=>

    ((lambda (a b c)
       (do-something-with a b c)
       (and-something-with b c))
     e1 e2 e3)


Of course, we could just give a name to the function, and call it:

    (defun body (a b c)
       (do-something-with a b c)
       (and-something-with b c))

    (body e1 e2 e3)


or:

    (if conditionp
       (body e11 e21 e31)
       (body e12 e22 e32))

But since your 'body' function is quite specific, there's no reason to
give it a global name.  Hence using flet, to give it a local name.


>> Don't put spaces between openning or closing parentheses (you
>> may put a space between a closing and an opening one).

Have a look at:

http://labs.ariel-networks.com/cl-style-guide.html
http://mumble.net/~campbell/scheme/style.txt
http://google-styleguide.googlecode.com/svn/trunk/lispguide.xml


>> In general use paredit ...

It lets you mostly ignore those style questions, since emacs does almost
all the work for you.

http://www.emacswiki.org/emacs/ParEdit
http://www.emacswiki.org/emacs/PareditCheatsheet
http://mumble.net/~campbell/emacs/paredit.el



>> In general, try to avoid setq, you can only write better code if
>> you can avoid setq or setf.  Of course, setf is often needed and
>> useful too, you have to know when ;-) But a (let (var) (setq var
>> expr)) is almost never a good expression.

setf does all that setq does, so you can just always use setf.

and you cannot do that with setq:

    (let ((x (cons 1 2)))
       (setf (car x) 42) 
       x)
    --> (42 . 2)

this is equivalent to:

    (let ((x (cons 1 2)))
       (rplaca x 42) 
       x)
    --> (42 . 2)


But setf is an operator that mutates variables or data objects.  Of
course, the purpose of an editor is to mutate the state of a buffer, so
we're quite far from functional programming.  Nonetheless, if you write
your emacs lisp code with a mostly functional programming style, it will
be good, since this has a lot of advantages:

http://www.emacswiki.org/emacs/FunctionalProgramming
http://c2.com/cgi/wiki?AdvantagesOfFunctionalProgramming
http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.  
You know you've been lisping too long when you see a recent picture of George 
Lucas and think "Wait, I thought John McCarthy was dead!" -- Dalek_Baldwin


  reply	other threads:[~2013-06-15 18:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-15  4:00 Moving between headers in message-mode Emanuel Berg
2013-06-15 12:58 ` Pascal J. Bourguignon
2013-06-15 17:02   ` Emanuel Berg
2013-06-15 18:22     ` Pascal J. Bourguignon [this message]
2013-06-18 22:17       ` Emanuel Berg
2013-06-19  0:30         ` Emanuel Berg
2013-06-19 15:02 ` Stefan Monnier
2013-06-20  9:32   ` Emanuel Berg
2013-06-20 13:16     ` Stefan Monnier
     [not found]     ` <mailman.2075.1371734198.22516.help-gnu-emacs@gnu.org>
2013-06-20 18:17       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hagzfd4k.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.