Dan Torop

Part 1: talking to Lisp, basics

Type M-x ielm to get into the interactive Lisp mode

You should see something like:

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> 

To test it out

type a number or some text in quotes, and it should echo that back to you:

ELISP> 23
23
ELISP>  "testing"
"testing"
ELISP> 

Setting variables with setq

ELISP> (setq foo 25)
25
ELISP> (+ foo 1)
26
ELISP> foo
25
ELISP> (setq foo (+ foo 1))
26
ELISP> foo
26
ELISP> 

Looping with while (from scratch)

ELISP> (setq foo 10)
10
ELISP> (while (> foo 0)
	 (insert "testing")
	 (newline)
	 (setq foo (- foo 1)))
nil
ELISP> testing
testing
testing
testing
testing
testing
testing
testing
testing
testing

Printing with loops within loops with dotimes

ELISP> (dotimes (outer-count 10)
	 (dotimes (inner-count outer-count)
	   (insert "?"))
	 (insert "! "))
nil
ELISP> ! ?! ??! ???! ????! ?????! ??????! ???????! ????????! ?????????! 

Asking for a response

ELISP> (setq foo (read))

Using defun

ELISP> (defun repeater (num-times print-what)
	 (dotimes (counter num-times)
	   (insert print-what)))
repeater
ELISP> (repeater 10 "!?")
nil
ELISP> !?!?!?!?!?!?!?!?!?!?

To run a defun or other Lisp outside of ielm, type M-: to get a Lisp prompt at the bottom of the screen, then type the Lisp you want. Press up-arrow to look for previous commands you typed.

If Emacs reports an error, you’ll see a Backtrace buffer appear which says Debugger entered on its top line. Type q to get rid of that, and try again.

More in Emacs Lisp programming pt. 2.