help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: conditionals in elisp


From: Drew Adams
Subject: RE: conditionals in elisp
Date: Tue, 27 Oct 2009 00:37:38 -0700

>> If you only partially understand what you read, try
>> something based on what you think you understand:
>> `M-:', then type some expression you think might
>> work, from what you read. Emacs and Emacs Lisp are
>> 100% interactive - you get immediate feedback.
>> Best way to learn.
>> 
>> If you don't understand what you read at all, and
>> you need more help, ask here. There's no shame in
>> asking, ever. But for your _own_ sake, learn to
>> interact with Emacs itself. You will not regret it.

> I can say for certain that after just half a page under 10.2
> Conditionals and my head is spinning.
> The stuff there does not match up to what little programming I've
> learned of perl shell etc.  Or if it does... I'm not seeing it yet.

OK. I apologize for assuming that you were looking for a quick answer here
instead of looking to learn.

Learning from examples is fine - very good, in fact. Others have already
provided you examples, so I hope that helped. The quick and easy answer would
have been to just give you a couple examples to learn from. You can evaluate
each expression by typing it after `M-:' and hitting Enter.

(if) raises an error: wrong number of arguments - 0 
(if nil) raises an error: wrong number of args - 1
(if nil 3)                -> nil
(if t   3)                -> 3
(if nil 3 4)              -> 4
(if t   3 4)              -> 3
(if nil 3 4 5)            -> 5
(if t   3 4 5)            -> 3
(if nil 3 4 5 6)          -> 6
(if "anything but nil" 3) -> 3
(if "nil" 3)              -> 3
(if 99 3 "anything")      -> 3
(if nil 3 (setq a 5) a)   -> 5

And maybe some more would help. But I'm sure others have helped enough on this
score.

The point remains, however: to learn, you need to dialog with Emacs itself. Not
to try that is to not learn the first thing about Emacs: it's helpful and
interactive.

Even if reading manuals is not your thing, you need to learn how Emacs itself
can help you: how to find the value of a variable; how to find what the
arguments to a function are; and so on. `C-h v' and `C-h f' are your friends.
These and other friends are all in the `Help > Describe' menu.

`M-:' is your friend. You can use it to evaluate any Emacs-Lisp expression, just
to see what happens. Try it - you can't break anything. If you find yourself in
the debugger because evaluation raised an error, just hit `q' to get back out.
Try it:

M-: (if)
M-: (if t)
M-: (if some-variable)

The error messages can also help you learn. Try, fail, succeed, fail, learn.
Know too that `C-g' cancels the action in progress and any crazy state you might
find yourself in - it returns you to the top level, where you want to be. Repeat
it if necessary: `C-g C-g'...

Even so, you will do yourself a big favor if you make occasional forays into the
Emacs manual, and even the Elisp manual. Run away when it seems to be gibberish,
sure, but keep diving back into it - some places are easier than others. Things
that are more helpful when you're starting out are generally nearer the
beginning of the manual.

In this case, if the `Conditionals' page was gibberish to you, try to understand
why. Maybe you lack some more basic understanding, so it would help to first get
some info about more basic stuff in Emacs Lisp. What does a truth (boolean)
value look like in Lisp, for instance? What are the paretheses all about? What
does a string look like? a variable?

There are a lot of such basic questions that are helpful places to start. And
yes, the manual is your friend for such things. In this case, asking the
manual's index about `true' would help you: `i true' takes you to the page that
explains truth values in Emacs Lisp, page `nil and t'. `i false' takes you to
the same page. The page is short, and I'll bet it clears up a few questions you
might have.

But if even that page is too hard, then ask Emacs questions about the terms
there that seem confusing to you. Ask about `variable'. Ask about `list'. Ask
about `quote'. You get the idea. The answers to your questions are really there,
and in some cases in language that you can understand. The problem is to find
the right pages to read - pages that make sense to you at your current level of
knowledge/ignorance. Skip over any stuff you cannot yet understand, and try to
find stuff you do understand.

But to do that you need, as a start, to feel comfortable asking Emacs. So the
first step is to find out how to ask Emacs. That's why I stressed learning that
first - before trying to learn Emacs Lisp. I don't mean learning Emacs; I
meaning learning to use Emacs to ask Emacs itself questions. Then you can ask it
about Emacs Lisp, for example.

`C-h i' is your friend; it puts you into a manual. But how to get around inside
a manual? If that's a problem, then start with the manual about reading manuals:
`Info'. (It's a bit verbose, but it can help.) In general, `i' (the index) is
your best friend inside a manual. `i variable', `i list' `i quote', `i
evaluation', even `i give up' - Emacs lets you know you cannot give up: "No
`give up' in index" - there ain't no such thing.

If the language and formatting of the Elisp manual is itself confusing, see page
`Conventions' (`g conventions'; `i conventions' won't get you there,
unfortunately). Page `nil and t' is part of the `Conventions' explanation.

You can also start with the Emacs tutorial: `C-h t' (menu Help > Emacs
Tutorial). That leads you through practice exercises. Again, it's a bit verbose,
but it can help.

Believe me, my intent was not to just throw the book at you, but to help. You
need to interact with Emacs itself _about learning Emacs_ and Lisp - that's the
key. There is nothing Emacs likes to talk more about than itself. And there's no
one who knows Emacs better than Emacs knows itself. The doc is sometimes wrong
or poorly worded, but `M-:' never lies.

A lot of Emacs's explanations will not be verbose gibberish. Explore the `Help >
Describe' menu, and you will see that much of that help is short and to the
point. `C-h b' gives you a list of all key bindings currently in effect - few
wasted words. `C-h k' tells you what any key, mouse action, or menu item does.

The first thing to try to find out about Emacs is not the syntax of particular
Emacs-Lisp expressions. It's how Emacs can help you find out about Emacs and
Lisp. Start with the `Help' menu (or the tutorial or the `Info' manual).

And please continue to ask questions here. Don't misunderstand my reply as
simply `RTFM'. My message is to bring Emacs itself into your circle of friends -
s?he can help; you just need to know how to ask.

 





reply via email to

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