Department of Mathematics FAS Harvard University One Oxford Street Cambridge MA 02138 USA Tel: (617) 495-2171 Fax: (617) 495-5132
FAS Computer Services to Harvard University IT: (617) 495-7777.
LISP is a high level, object-oriented language for Artificial Intelligence (AI) applications. Similar as Python or Mathematica, it is a dynamic language, you don't need to compile it. Because all work is done via function composition and recursion, it is also called a functional language.
Terminology:
  • An atom is either an integer or an identifier.
  • A list is a left parenthesis, followed by zero or more S-expressions, followed by a right parenthesis. An S-expression is an atom or a list.
  • NIL means "false" and is also the name for the empty list ( ). NIL is both an atom and a list.
  • T means "true". Actually anything that is not NIL is also "true".
Starting up clisp: clisp a Common Lisp implementation which supports the ANSI Common Lisp standard. (we have installed clisp 2.28 on Abel).
  • You start it up (for example on Abel) by typing 'clisp' in the shell.
  • You end it in clisp by typing '(exit)'.
A session Start up lisp and try to figure out, what each of the following lines does: the first group show how lisp works with numbers, the two functions factor and ulam show how to define functions and loops, then follow some information on how to work with lists.
(exp (+ 6.23 1))
(sin (* 5 2))
(+ 3 4/5 7 9 10)
(* (+ 3 4) (+ (/ 4 5) 6))    

(defun fact (x) (if (> x 0) (* x (fact (- x 1))) 1) ) 
(fact 5)

(defun ulam (n steps) 
   (cond
     ((= n 1) steps)
     ((oddp n) (ulam (+ 1 (* n 3)) (+ 1 steps)))
     (t (ulam (/ n 2) (+ 1 steps)))
   ) 
)
(ulam 11 0)

(list a b c d)
(dolist (w '(a b c d e f g h)) (print w))
(concatenate 'string "mathematics " "department")
(sort '(1 2 5 4 3 7 6) #'<)
(sort '(1 2 5 4 3 7 6) #'>)
(reverse '(1 2 3 4 5))
(append '(1 2 3) '(4 5 6)) 
(intersection '(a b c d e) '(b c f))
(union '(a b) '(c d)) 

(if 4 5 6)
(if NIL 5 6)
(eq 'a 'a)
(eq 'a 'b)
(= 3 4)

(setq a nil)
(push 4 a)
(pop a)

(cons 8 11) 
(car (cons 8 11))
(cdr (cons 8 11))
Links:


Simplicity, Clarity, Generality B.W. Kernighan, R. Pike, in "The Practice of Programming".
Privacy
HTML CSS