Frank Schacherer Homepage
home technology bibliography

Lisp

[Um oh yeah.]

I always wanted to learn the language that needs syntax highlighting to be intelligble at all. Lisp is a functional language instead of a declarative one, for a change. Here are some scribble notes on EMACS Lisp, which conveniently comes with my editor.

Useful lisp control structures and idioms


Control structures
(if CONDITION THEN-BODY ELSE-BODY)
(when CONDITION WHEN-BODY)
(cond                       ;; lisps switch
   (CONDITION BODY)
   (CONDITION2 BODY2) ...)

(eq A B) ;; true if the same object (char literals, numbers etc)
(equal A B) ;; true if objects have same components (lists, strings)
and, or, not ;; combine conditions
everything that evaluates to nil or () (empty list, which is nil, too) is false
everything else is true; t is true


Function definition
(defun NAME (ARG_LIST)
  "Commentary,
which may span several lines
and respects indentation."
   (interactive ["r"]) ;; if it is an interactive function
                       ;; * signal error if buffer is read-only 
                       ;; p take numeric prefix arg
                       ;; r take point and mark as params, smallest first (region) 
   ;;; here comes the actual function body
   )
Saving the current environ(buffer, point, mark)
(save-excursion
  ;;; code here
  )
Useful to wrap around the body of a function, so you
do not have to retore values by hand (and also works
if function crashes)

Local vars in functions 
(let ((varname1 (defining-function1))
      (varname2 (defining-function2)));; end of definition list
   ;;;code using local vars here
   )
(set 'symbol 'value) ;; quote value unless you want it eval'd
(setq symbol 'value) ;; the same, q stand for quoted symbol


Working with datatypes 
'(1 (2 (3))) ;; literal list
(list 1 2 3) ;; create a list
(concat SEQUENCES) ;; return a string; a SEQUENCE is a LIST, STRING or ARRAY 

numbers in a sequence are interpreted as the character with that
number. to get them as literals use (number-to-string)
For regexen that are represented as strings, a literal \
has to be ecaped twice, once for the regex, once for the
string, yielding "\\\\"


Useful Emacs variables and functions

Square brackets indicate optional arguments here. For full arguments and descriptions see the emacs documentation.

(point) ;; current position of point
(newline) ;; insert a newline
(goto-char POS) ;; move point to POS
(beginning-of-buffer), (beginning-of-line)
(end-of-buffer), (end-of-line)
(re-search-forward "re-string" [max-buffer-pos ret-nil-if-t repeat]) returns nil if no match found
(replace-match "newtext")
(kill-region BEG END)

(((vintage lisp)))

(progn A B C ...) ;; execute A, B, C in that order
                  ;; function bodies are an implicit progn
(cons A B) ;; build a cons cell from A and B
(car '(list)) ;; get the car of the first cons cell
(cdr '(list)) ;; get the cdr of the first cons cell

Last modified: Thu Feb 14 20:16:12 Westeuropäische Normalzeit 2002