From: Tim X <timx@nospam.dev.null>
To: help-gnu-emacs@gnu.org
Subject: Re: Elisp Tutorial dumb question -- but I thought I better doublecheck ??
Date: Wed, 25 Apr 2007 16:14:29 +1000 [thread overview]
Message-ID: <87fy6pvv3u.fsf@lion.rapttech.com.au> (raw)
In-Reply-To: mailman.2465.1177432345.7795.help-gnu-emacs@gnu.org
William Case <billlinux@rogers.com> writes:
> Subject: Elisp Tutorial dumb question -- but I thought I better doublecheck ??
>
> Hi;
>
> I am working my way through the elisp tutorial
> at :http://www.linuxselfhelp.com/gnu/emacs-lisp-intro/html_mono/emacs-lisp-intro.html#Writing%20Defuns
>
> Section 3.3 on defuns gives an algorithm for the basic defun as:
>
> defun
> (defun function-name (arguments ... )
> "optional-documentation ..."
> (interactive argument-passing-info)
> body ... )
>
> and later gives an algorithm for the lambda anonymous function as:
> C.4.3 A lambda Expression: Useful Anonymity
>
> (lambda (arg-variables...)
> [documentation-string]
> [interactive-declaration]
> body-forms...)
>
> The differences seem trivial, but can I re-write the lambda algorithm in
> terms of the defun algorithm for myself such that:
>
> lambda
> (lambda (arguments ... )
> "optional-documentation ..."
> (interactive argument-passing-info)
plus
body-forms....)
If I understand things correctly, the answer is sort of yes. The difference
between [interactive ...] and (interactive ....) in the two definitions seems
inconsistent to me because the [...] notation indicates optional features.
On the other hand, 'interactive' is just another body form in both the defun
and lambda. Its just listed separately because it performs a special role.
'interactive' is only required in a defun when it is to be an interactive
command. Likewise, interactive is only required in a lambda expression if it is
to be called interactively. However, the big difference is that you cannot call
a lambda expression interactively without first associating it with some
symbol/name. Generally, you will see lambda expressions in places where
functions can be if the expression is only going to be used once. for example,
you often see things like
(add-hook 'my-mode-hook (lambda ()
(do-something-1)
(do-somethint-2)))
instead of
(defun my-hook-func ()
(do-something1)
(do-something2))
(add-hook 'my-mode-hook 'my-hook-func)
With the second approach, there is now an interned symbol called my-hook-func
that is never going to get used again. In the first example, the same outcome
is achieved, but without creating (polluting) the namespace with a symbol which
will never be used again. This same approach is often seen with functions that
take a function as an arguement, like mapcar.
Since lambda expressions/functions are used in this way, it is rare to see them
include a document string (document strings often just explain what the
function does and how it is called, but as a lambda is not usually going to be
called in other places, such information is rarely useful.
the 'interactive' function is also rarely seen in a lambda for the same
reasons. Putting (interactive) in your function definition means the function
can now be used as a command (i.e. called with M-x func-name or interactively
via a key binding). However, as lambda functions are by definition anonymous
and don't have a name, they are generally of no use in a M-x situation. There
are ways of associating a lambda function/expression with a symbol - sort of
giving them a name, but this is usually only needed in very special situations
and something you can ignore when learning.
There are a few other subtle differences between a regular function and a
lambda expression, but initially, when learning this stuff, you can safely
ignore this. Later, as your understanding grows, these differences will make
more sense and will be easier to understand.
Note that in emacs lisp, you want to try and minimise the number of distinct
symbols to avoid namespace pollution. Unlike some lisps that have package
namespaces, emacs lisp doesn't. this is why you see a lot of function names
that have a package prefix in emacs lisp, but not in common lisp etc. If you
intern too many symbols, you begin to run out of options for new meanigful ones
or start shadowing/redefining existing functions and all sorts of weird things
begin to happen.
Tim
--
tcross (at) rapttech dot com dot au
next prev parent reply other threads:[~2007-04-25 6:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.2465.1177432345.7795.help-gnu-emacs@gnu.org>
2007-04-25 4:59 ` Elisp Tutorial dumb question -- but I thought I better doublecheck ?? Barry Margolin
2007-04-25 6:14 ` Tim X [this message]
2007-04-25 8:26 ` Maciej Katafiasz
[not found] ` <mailman.2497.1177490263.7795.help-gnu-emacs@gnu.org>
2007-04-25 11:43 ` Tim X
2007-04-24 16:23 William Case
2007-04-25 8:20 ` Maciej Katafiasz
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fy6pvv3u.fsf@lion.rapttech.com.au \
--to=timx@nospam.dev.null \
--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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).