Transcript LISP

LISP
LISt Processing
1
History & Overview

One of the oldest high level programming languages.

First developed in 1958 by John McCarthy.

Later adopted as the primary research language of Artificial
Intelligence (AI).

The syntax of LISP is one of the least restrictive of any highlevel programming languages.
2
History & Overview

Was originally developed as an interpreted language, however,
a number of current versions of LISP make use of an
incremental compiler.

Among LISP's strongest features is that programs may be
treated as data.

In LISP, entire programs can be used as input to other programs
or sub-programs directly.
3
Grammar
• List
:: = ( X1 X2 … Xn)
•X
:: = List | Atom
• Atom
:: = Number | Name
• Number :: = . . . | -1024 | . . . | -2 | 1 | 0 | 1 | 2 | . . . | 1024 | . . .
• Name
:: = a sequence of alphanumeric characters
containing at least one letter and no separators
• Separator :: = . | space | ( | ) | [ | ] | ‘ | tab | carriage return
all are characters with a particular role.
4
Features/Parts of LISP
Definitions

Atom : A series of numbers or symbols that can’t be broken in to
smaller components.
Ex. Cat, Scott9, 684, 7Hearts, This-Is-A-Very-Long-Atom

List : A sequence of atoms or other lists enclosed by
parentheses
LIST
LENGTH
()
0
(John gives Mary a book)
5
((subject) (verb) (object))
3
((+ X Y) (+ A B))
2
(1 2 3 A B C C B A 3 2 1)
12
(()())
2

5
Features/Parts of LISP
Syntax

The basic syntax for LISP is : (expression)

Everything is expressed as a list or as part of a list, enclosed in
parentheses

Not case-sensitive

Supports recursion
6
Features/Parts of LISP
Syntax

The comment character is a semi-colon ‘;’ which can appear
anywhere. This results in the remainder of the line being
skipped (i.e. not being treated as code).

There are 2 main components of a list,
the head of the list and the rest of the list.

The head of the list OR the first iem is referred to as CAR.
The rest of the list is referred to as CDR.


(Note: CAR & CDR were register names from the IBM
mainframe that LISP was first written for.)
7
Features/Parts of LISP
Syntax

Many times the CAR is a function with its arguments being
CDR. The basic mathematical operations are a good example.
(+ 4 2)
(- 4 2)
(* 4 2)
(/ 4 2)

Note the syntax for all functions is simply a list of symbols and
numbers.

All LISP operations return a value. This value is the last value in
the list. For a procedure, the last value is the return or result
value.
8
List Manipulation
Some of the LISP primitives (built-in commands or functions) for
manipulating lists are:
 LIST - Returns a list of its arguments
> (list 1 2 3 4)
(1 2 3 4)
SETQ - Takes at least 2 arguments, the 1st being a variable name
and the 2nd being any LISP expression. LISP evaluates the
expression passed as the 2nd argument and assigns the value returned
as the new value of the variable passed as the 1st argument.
> (setq mylist (list 1 2 3 4)) > (setq yourlist (list 10 11 12 13))

(1 2 3 4)
(10 11 12 13)
9
List Manipulation

CAR - Returns the first element of a list
> (car mylist)
1

CDR - Returns a list of all elements except the first
> (cdr mylist)
(2 3 4)

Nth - Returns a specific list element
> (nth 3 mylist)
> (nth 2 yourlist)
4
12
10
List Manipulation

CONS - Short for concatenates.
Will insert a new element at the front of a list
> (cons (car mylist) (cdr mylist))
(1 2 3 4)
>
(cons (car mylist) (cdr yourlist))
(1 11 12 13)
>
(cons mylist yourlist)
((1 2 3 4) 10 11 12 13)
11
List Manipulation

APPEND - Adds new list elements to the end of a list
>
(append mylist yourlist)
(1 2 3 4 10 11 12 13)
12
Functions

DEFUN - This command is used to define functions which LISP
refers to as a value-returning procedures.
>
(
defun MyValueReturningProcedureName
(optional_input_variables)
ValueReturningProcedure_body
)
13
Input/Output

PRINT & PRINC - These will output an expression to the output
device followed by a space. PRINT adds a line-feed.

READ - This will read an expression from the input device.
14