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

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

Re: conditionals in elisp


From: tomas
Subject: Re: conditionals in elisp
Date: Tue, 27 Oct 2009 06:50:04 +0100
User-agent: Mutt/1.5.15+20070412 (2007-04-11)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Oct 26, 2009 at 07:08:13PM -0500, Harry Putnam wrote:
> I'm hoping someone will be kind enough to demonstrate a couple of
> brief conditionals in elisp... if and if else.

While the other posters sound harsh, they just are trying to provide you
with some fishing rods, instead of the fish you asked for (sorry, I
couldn't resist).

Lest you starve in the process, and because I have the suspicion that
there is just a little specific tangle between you and the docs, here is
a fish (with a bit of fishing line attached to it): in Lisp, everithing
is an expression. There are no "statements", as you might be accustomed
to from other languages. All those "control staatements" are just
expressions. If looks like that:

  (if lunchtime
    (eat fish)
   (work this)
   (work that)
   (procrastinate a-bit))

The meaning of that is: if the value of the variable "lunchtime" can be
interpreted as "true" (i.e. is not nil), evalate the first expression
(i.e. (eat fish). Otherwise evaluate the sequence coming after that
(i.e. first (work this) and so on). The whole thing returns a value (you
don't really say that in Lisp world: you rather say "the if expression
evaluates to"). This is the value of (eat fish) in the first case, the
value of the last expression in the else part (i.e. (procrastinate a-bit) 
in the second case.
 
Of course you may replace the variable lunchtime by a more grown-up
expression:

  (if (eq (current-time) 'lunchtime)
    (whatever)
   (others))

If you want to do more work than one expression, have a look at prog1
and progn, which allow you to wrap a whole sequence of expressions in
one.

Of course, the pointers given to you in this thread are all very
valuable. Especially, the tutorials by Xah mentioned will take you much
further than this, and peeking into all the sources provided with Emacs
can't be substituted by any well meaning post.

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFK5ooMBcgs9XrR2kYRAogWAJoChrDGFCWUpQvfqzcEUAYddO13MQCeJbkZ
vUsrlFC4J3iJ2sP9aHCPQcA=
=eocn
-----END PGP SIGNATURE-----




reply via email to

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