Intro from ACSL

LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of the most powerful. It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt Processing language”. Today, it is used for virtually all Artificial Intelligence programs and is the environment of choice for applications which require a powerful interactive working environment. LISP presents a very different way to think about programming from the “algorithmic” languages, such as BASIC, Fortran and Pascal.

As its name implies, the basis of LISP is a list. One constructs a list by enumerating elements inside a pair of parentheses. For example, here is a list with four elements (the second element is also a list):

(23 (this is easy) hello 821)

The elements in the list, which are not lists, are called “atoms.” For example, the atoms in the list above are: 23, this, hello, 821, easy, and is. Everything in LISP is either an atom or a list (but not both). The only exception is “NIL,” which is both an atom and a list. It can also be written as “()” – a pair of parentheses with nothing inside.

All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 … argn). To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments. For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35. Some functions have an arbitrary number of arguments; others require a fixed number. All statements return a value, which is either an atom or a list.

You can read more about lisp structure here, an excellent 4 pages on LISP lists

beautiful use of LISP: Andrew Sorensen at OSCON Andrew Sorensen at TEDxQUT

Syntax Summary

variables

Statementvalue
(SET 'a 3)3
(SETQ a 3)3
(EVAL '(ADD 3 4)7


lists

Statementvalue
(REVERSE '(1 2 3))(3 2 1)
(CAR '(1 2 3))1
(CDR '(1 2 3))(2 3)
(CONS '1 '(2 3))(1 2 3)


math

Statementvalue
(ADD 1 2 3) 6
(MULT 1 2 3)6
(SUB 1 2)-1
(DIV 1 2) 0.5
(SQUARE 3)9
(EXP 2 3) 8


tests

Statementvalue
(EQ 1 2) nil
(POS 2) true
(NEG 2) nil
(ATOM 5) true


functions

(defun function_name (arguments) ...)
(defun square(n) (MULT n n))

LISP REPL

:point_right: use (+ 3 4) not (add 3 4)

Some commands to try (can you predict the output?):

1.  (CAR `(This is a list))
2.  (CAR (CDR `(This is a list)))
3.  (SETQ x (CDR (CDR `(a b c d)))) 
4.  (CDR `(This is a list ))
5.  (CAR `(hi))
6.  (CDR `(one))
7.  (CAR `((1) (2 3) (4 5 6))) 
8.  (CONS 32 `(22 12))
9.  (CONS `first `(last))
10.  (CONS `(one) `(two three)) 
11.  (CDR (CAR `((red white) blue))) (CONS (CAR `(red green blue)) (CDR `(brown tan gold)))
12.  (CAR `hi)
13.  (CAR ())
14.  (CAR `(ADD 2 2))
15.  (CONS 78 NIL)
16.  (CDR `((red green) (blue white)))
17.  (SETQ a `(orange black)) 
18.  (ATOM (SETQ b (CONS `red a)))
19.  (ATOM b)
20.  (ATOM (CAR b))
21.  (ATOM NIL)
22.  (SETQ fruit `(apple orange)) 
23.  (SETQ more (CONS `grape fruit)) 
24.  (CDR (CDR (REVERSE more))) 
25.  (SETQ colors `(red green yellow)) 
26.  (SETQ area (MULT 2 3))
27.  (SETQ A (CONS area `(CDR colors))) 
28.  (SETQ B (CONS `area (CDR colors))) 
29.  (SETQ C (REVERSE colors)) 
30.  (CAR (CDR C)) 
31.  (CONS `pink (CONS `orange (CONS C NIL)))