Tuesday, July 7, 2009

Basic Functions in (emacs) lisp

In this post, I summarise a few functions in emacs lisp and give a few example programs. Do try them out in your emacs editor and leave your comments if you are not able to understand something.

Function Name Syntax Input form Output form
quote or '
(' is a short form of quote)
(quote arg1) or 'arg1 arg1 is any symbol return the symbol arg1
without modification
+ (+ a1 a2 ... an) 'n' numbers a1,a2 ... an the sum of a1,a2, ... ,an
=> (a1 + a2 + .. +an)
* (* a1 a2 ... an) 'n' numbers a1,a2 ... an product of a1 a2 a3 ...an
=> (a1 * a2 * ... *an)
- (- a1 a2 ... an) 'n' numbers a1,a2 ... an subtract from a1,the sum
of a2,a3,... an
=>(a1 - (a2 + a3 + ... an))
/ (/ a1 a2 ... an) 'n' numbers a1,a2 ... an Divide a1 by the product,
of a2,a3 ... an
=>(((a1/a2)/a3).../an) or
=>(a1/(a2*a3*...an))
% (% a1 a2) 2 numbers a1,a2 Return the remainder when a1 is divided by a2
=>(a1 % a2)

Now onto some example programs that use the functions described above. Do evaluate them and see for yourself.
Code :
  1. (quote 5)
  2. (quote (+ 1 2))
  3. (+ 1 2)
  4. (+ 1 (+ 2 3))
  5. (- 100 (+ 20 5))
  6. (% (* 10 5) (+ 5))
  7. (* (-) (+))
  8. (quote (+ 1 2 (+ 3 4)))

When you evaluate the above programs in emacs you will get the following results :
  1. (quote 5) => 5
  2. (quote (+ 1 2)) => (+ 1 2)
  3. (+ 1 (+ 2 3)) => (+ 1 5) => 6
and so on. You may try the other programs and also think of some programs of your own and evaluate them.

0 comments:

Post a Comment