Lisp Tutorial PDF
Lisp Tutorial PDF
This tutorial takes you through features of LISP Programming language by simple and
practical approach of learning.
Audience
This reference has been prepared for the beginners to help them understand the basic
to advanced concepts related to LISP Programming language.
Prerequisites
Before you start doing practice with various types of examples given in this reference,
we assume that you are already aware of the fundamentals of computer programming
and programming languages.
All the content and graphics published in this e-book are the property of Tutorials Point
(I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or
republish any contents or a part of contents of this e-book in any manner without
written consent of the publisher.
You strive to update the contents of our website and tutorials as timely and as precisely
as possible, however, the contents may contain inaccuracies or errors. Tutorials Point
(I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness
of our website or its contents including this tutorial. If you discover any errors on our
website or in this tutorial, please notify us at [email protected]
i
LISP
Table of Contents
About the Tutorial ............................................................................................................................................. i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
1. OVERVIEW................................................................................................................................... 1
2. ENVIRONMENT SETUP................................................................................................................. 3
3. PROGRAM STRUCTURE................................................................................................................ 4
ii
LISP
6. MACROS .................................................................................................................................... 14
7. VARIABLES ................................................................................................................................. 15
8. CONSTANTS ............................................................................................................................... 18
9. OPERATORS ............................................................................................................................... 19
Arithmetic Operations.................................................................................................................................... 19
iii
LISP
Mapping Functions......................................................................................................................................... 45
17. STRINGS..................................................................................................................................... 66
iv
LISP
v
LISP
Opening Files.................................................................................................................................................115
Using a Package.............................................................................................................................................123
vi
LISP
Inheritance ....................................................................................................................................................136
vii
1. OVERVIEW LISP
LISP stands for LISt Programming. John McCarthy invented LISP in 1958, shortly
after the development of FORTRAN. It was first implemented by Steve Russell on an
IBM 704 computer. It is particularly suitable for Artificial Intelligence programs, as it
processes symbolic information efficiently.
Common LISP originated during the decade of 1980 to 1990, in an attempt to unify
the work of several implementation groups, as a successor of Maclisp like ZetaLisp
and New Implementation of LISP (NIL) etc.
It is expression-based.
8
LISP
G2
AutoCad
Igor Engraver
Yahoo Store
9
2. ENVIRONMENT SETUP LISP
CLISP is the GNU Common LISP multi-architechtural compiler used for setting up
LISP in Windows. The Windows version emulates Unix environment using MingW
under Windows. The installer takes care of this and automatically adds CLISP to the
Windows PATH variable.
http://sourceforge.net/projects/clisp/files/latest/download
It creates a shortcut in the Start Menu by default, for the line-by-line interpreter.
clisp hello.lisp
10
3. PROGRAM STRUCTURE LISP
Atoms
Lists
Strings
The interpreter checks the source code in a repeated loop, which is also called the
Read-Evaluate-Print Loop (REPL). It reads the program code, evaluates it, and prints
the values returned by the program.
(+7911)
27
If you would like to execute the same program as a compiled code, then create a
LISP source code file named myprog.lisp and type the following code in it:
(write(+7911))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
27
11
LISP
a * ( b + c ) / d
(/ (* a (+ b c) ) d)
Let us take another example. Let us write code for converting Fahrenheit temperature
of 60o F to the centigrade scale:
(60 * 9 / 5) + 32
Create a source code file named main.lisp and type the following code in it:
When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately
and the result is:
140
12
LISP
The evaluator defines syntax of LISP forms that are built from s-expressions.
The evaluator works as a function that takes a valid LISP form as an argument
and returns a value. This is the reason why we put the LISP expression in
parenthesis, because we are sending the entire expression/form to the
evaluator as argument.
Let us create new source code file named main.lisp and type the following code in it:
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
Hello World
I am at 'Tutorials Point'! Learning LISP
13
4. BASIC SYNTAX LISP
atom
list
string
hello-from-tutorials-point
name
123008907
*hello*
Block#221
abc123
A list is a sequence of atoms and/or other lists enclosed in parentheses. The following
examples show some valid lists:
( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )
14
LISP
" I am a string"
"a ba c d efg #$%^&!"
"Please enter the following details:"
"Hello from 'Tutorials Point'! "
Adding Comments
The semicolon symbol (;) is used for indicating a comment line.
Example
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result returned is:
Hello World
I am at 'Tutorials Point'! Learning LISP
Notable Points
The following important points are notable:
LISP represents a function call f(x) as (f x), for example cos(45) is written as
cos 45
LISP expressions are not case-sensitive. Means, cos 45 or COS 45 are same.
o Numbers
o The value nil, that stands for logical false, as well as an empty list.
15
LISP
LISP Forms
In the previous chapter, we mentioned that the evaluation process of LISP code takes
the following steps:
The evaluator defines syntax of LISP forms that are built from s-expressions.
This second level of evaluation defines a syntax that determines which s-
expressions are LISP forms.
An atom
The evaluator works as a function that takes a valid LISP form as an argument and
returns a value. This is the reason why we put the LISP expression in
parenthesis, because we are sending the entire expression/form to the evaluator as
argument.
A name can have digits but must not be made of only digits, because then it would
be read as a number. Similarly a name can have periods, but cannot be entirely made
of periods.
At times, we need to take atoms or lists literally and do not want them evaluated or
treated as function calls. To do this, we need to precede the atom or the list with a
single quotation mark.
16
LISP
Create a file named main.lisp and type the following code into it:
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
17
5. DATA TYPES LISP
Any variable can take any LISP object as its value, unless you declare it explicitly.
Although, it is not necessary to specify a data type for a LISP variable, however, it
helps in certain loop expansions, in method declarations and some other situations
that we will discuss in later chapters.
The data types are arranged into a hierarchy. A data type is a set of LISP objects and
many objects may belong to one such set.
The typep predicate is used for finding whether an object belongs to a specific type.
18
LISP
Apart from these system-defined types, you can create your own data types. When
a structure type is defined using defstruct function, the name of the structure type
becomes a valid type symbol.>/p>
Example 1
Create new source code file named main.lisp and type the following code in it:
(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print x)
(print y)
(print n)
(print ch)
(print bg)
(print r)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result returned is:
10
34.567
123.78
NIL
19
LISP
110000.0
62
Example 2
Next let us check the types of the variables used in the previous example. Create
new source code file named main.lisp and type the following code in it:
(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print (type-of x))
(print (type-of y))
(print (type-of n))
(print (type-of ch))
(print (type-of bg))
(print (type-of r))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
(INTEGER 0 281474976710655)
SINGLE-FLOAT
SINGLE-FLOAT
NULL
SINGLE-FLOAT
(INTEGER 0 281474976710655)
20
LISP
21
6. MACROS LISP
Defining a Macro
In LISP, a named macro is defined using another macro named defmacro. Syntax
for defining a macro is:
The macro definition consists of the name of the macro, a parameter list, an optional
documentation string, and a body of LISP expressions that defines the job to be
performed by the macro.
Example
Let us write a simple macro named setTo10, which takes a number and sets its value
to 10.
Create new source code file named main.lisp and type the following code in it:
defmacro setTo10(num)
(setq num 10)(print num))
(setq x 25)
(print x)
(setTo10 x)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
22
LISP
25
10
23
7. VARIABLES LISP
In LISP, each variable is represented by a symbol. The name of the variable is the
name of the symbol and it is stored in the storage cell of the symbol.
Global Variables
Global variables are generally declared using the defvar construct. Global variables
have permanent values throughout the LISP system and remain in effect until new
values are specified.
Example
(defvar x 234)
(write x)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
234
As there is no type declaration for variables in LISP, you need to specify a value for
a symbol directly with the setq construct.
Example
->(setq x 10)
The above expression assigns the value 10 to the variable x. You can refer to the
variable using the symbol itself as an expression.
The symbol-value function allows you to extract the value stored at the symbol
storage place.
Example
Create new source code file named main.lisp and type the following code in it:
(setq x 10)
(setq y 20)
24
LISP
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
x = 10 y = 20
x = 100 y = 200
Local Variables
Local variables are defined within a given procedure. The parameters named as
arguments within a function definition are also local variables. Local variables are
accessible only within the respective function.
Like the global variables, local variables can also be created using the setq construct.
There are two other constructs - let and prog for creating local variables.
Where var1, var2,…,varn are variable names and val1, val2,…, valn are the initial
values assigned to the respective variables.
When let is executed, each variable is assigned the respective value and at last, the s-
expression is evaluated. The value of the last expression evaluated is returned.
If you do not include an initial value for a variable, the variable is assigned to nil.
Example
Create new source code file named main.lisp and type the following code in it:
25
LISP
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and
the result is:
x = A y = B z = C
The prog construct also has the list of local variables as its first argument, which is
followed by the body of the prog, and any number of s-expressions.
26
LISP
27