2. TOPIC TO BE DISCUSSED
An INTRODUCTION OF LISP ?
FEATUERS OF LISP
TEXT EDITOR
LISP EXECUTOR
LISP – BASIC SYNTAX
LISP-DATA TYPES
LISP CONSTANT
LISP VARIABLES
LISP OPERATOR
DECISION MAKING
LISP ARRAY
LISP LOOP
3. AN INTRODUCTION OF LISP ?
•Lisp – List Processing language
• John McCarthy invented LISP in 1958, shortly after the development of
FORTRAN. It was first implement by Steve Russell on an IBM 704 computer
• It is one of the most popular language for Artificial Intelligence programs
• FEATURES OF LISP:
• It is machine-independent
• It allows updating the programs dynamically.
• It provides high level debugging.
4.
5. FEATUERS OF LISP
• It provides wide-ranging data types like, objects, structures, lists, vectors, adjustable
arrays, hash-tables, and symbols.
• It provides an object-oriented condition system.
• It provides complete I/O library.
• It provides extensive control structures.
• Lisp was originally created as a practical mathematical notation for computer programs
6. USES OF COMMON LISP
USES OF CL
• CL is a very successful language in that
it is extremely powerful as an AI tool
those that use it love it
• And yet CL is a very uncommon language because
it is very complicated
most software development is in C++ (with some in Java, C#, C, Python, Ada, etc)
such that few programmers would choose to use CL for development
• The most important software developed from CL includes:
– Emacs
– G2 (a real-time expert system)
– AutoCAD
– Igor Engraver (musical notation editor and publisher)
– Yahoo Store
7. TEXT EDITOR
• This will be used to type your program. Examples of few editors include Windows
Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.
• Name and version of text editor can vary on different operating systems. For example,
Notepad will be used on Windows, and vim or vi can be used on windows as well as
Linux or UNIX.
• The files you create with your editor are called source files and contain program source
code. The source files for Lisp programs are typically named with the extension ".lisp".
• Before starting your programming, make sure you have one text editor in place and
you have enough experience to write a computer program, save it in a file, finally
execute it.
8. TEXT EDITOR
• The source code written in source file is the human readable source for your program.
It needs to be "executed", to turn into machine language so that your CPU can actually
execute the program as per instructions given.
• This Lisp programming language will be used to execute your source code into final
executable program. I assume you have basic knowledge about a programming
language.
• CLISP is the GNU Common LISP multi-architechtural compiler used for setting up
LISP in Windows.
10. LISP - BASIC SYNTAX
LISP programs are made up of three basic building blocks:
• atom
• list
• string
ATOM:
An atom is a number or string of contiguous characters. It includes numbers and special
characters.
12. DEFINE LIST
LIST
A list is a sequence of atoms and/or other lists enclosed in parentheses
EXAMPLE
(I AM A GENIOUS GIRL)
(I AM A LIST)
(A(A B C)D )
13. DEFINE STRING
STRING
A string is a group of characters enclosed in double quotation marks
EXAMPLE
“I AM A STRING”
“ HELLO WORLD”
“PLEASE ENTER THE FOLLOWING DETAILS”
14. ADDING COMMENTS
COMMENTS
The semicolon symbol (;) is used for indicating a comment line
EXAMPLES
“I AM A STRING”
; HELLO WORLD
“PLEASE ENTER THE FOLLOWING DETAILS”
15. CONCEPTS ABOUT LISP
Following are some of the important points to note
The basic numeric operations in LISP are +, -, *, and /
LISP expressions are case-insensitive
Naming Conventions in LISP
• Name or symbols can consist of any number of alphanumeric characters other than
whitespace, open and closing parentheses, double and single quotes, backslash, comma,
colon, semicolon and vertical bar. To use these characters in a name, you need to use
escape character ().
• A name can have digits but not entirely made of digits, because then it would be read as a
number. Similarly a name can have periods, but can't be made entirely of periods.
16. LISP DATA TYPES
LISP data types can be categorized as
• Scalar types - for example, number types, characters, symbols etc.
• Data structures - for example, lists, vectors, bit-vectors, and strings.
17. LISP VARIABLES
Global Variables
• Global variables have permanent values throughout the LISP system and remain in
effect until a new value is specified.
• Global variables are generally declared using the defvar construct.
Example
(Defvar X 234)
(Write x)
output: (234)
• Since there is no type declaration for variables in LISP, you directly specify a value
for a symbol with the setq construct
• Example
(set q x 10 )
(format t "x = ~2d ~%" x )
• The above expression assigns the value 10 to the variable x
output: (x = 10)
18. LISP VARIABLE
LOCAL VARIABLE
• 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 setqconstruct.
• There are two other constructs - let and prog for creating local variables.
EXAMPLE:
(let ( (x’ a) (y ’b) (z’ c)))
Output: x = A y = B z = C
(prog ((x '(a b c))(y '(1 2 3))(z '(p q 10)))
Output : x = (A B C) y = (1 2 3) z = (P Q 10)
19. LISP CONSTANT
CONSTANT
In LISP, constants are variables that never change their values during program
execution.
Constants are declared using the defconstant construct.
EXAMPLE
PI value is 3.14
Value cannot be changed
20. LISP OPERATOR
OPERATOR
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
The operations allowed on data could be categorized as:
• Arithmetic Operations
• Comparison Operations
• Logical Operations
• Bitwise Operations
21. LISP OPERATOR
ARITHMETIC OPERATORS
IF VARIABLE A HOLDS 10 AND VARIABLE B HOLDS 20
DESCRIPTION EXAMPLE
+ Adds two operands (+AB) will give 30
- Subtracts second operand from the first (- A B) will give -10
* Multiplies both operands
(* A B) will give 200
22. LISP OPERATOR
OPERATOR DESCRIPTION EXAMPLE
/
Divides numerator by de-numerator
(/ B A) will give 2
Mod,rem Modulus Operator and remainder of after an
integer division
(mod B A )will give
0
incf Increments operator increases integer value
by the second argument specified
(incf A 3) will give
13
decf Decrements operator decreases integer
value by the second argument specified
(decf A 4) will give 9
23. COMPARISON OPERATOR
OPERATO
R
DESCRIPTION EXAMPLE
= Checks if the values of the operands are
all equal or not, if yes then condition
becomes true.
(= A B) is not true.
/= Checks if the values of the operands are
all different or not, if values are not equal
then condition becomes true.
(/= A B) is true.
Assume variable A holds 10 and variable B holds
20, then:
24. COMPARISON OPERATOR
> Checks if the values of the operands are monotonically decreasing. (> A B) is not true.
< Checks if the values of the operands are monotonically increasing. (< A B) is true.
>= Checks if the value of any left operand is greater than or equal to the
value of next right operand, if yes then condition becomes true.
(>= A B) is not true.
25. COMPARISON OPERATOR
<= Checks if the value of any left operand is less than
or equal to the value of its right operand, if yes then
condition becomes true.
(<= A B) is true.
max It compares two or more arguments and returns
the maximum value.
(max A B) returns 20
min It compares two or more arguments and returns
the minimum value.
(min A B) returns 20
26. LOGICAL OPERATION ON BOOLEAN
VALUES
Common LISP provides three logical operators: and, or, and not that operates on
Boolean values. Assume A has value nil and B has value 5, then:
OPETATOR DESCRIPTION
EXAMPLE
and It takes any number of arguments. The arguments are
evaluated left to right. If all arguments evaluate to non-nil, then
the value of the last argument is returned. Otherwise nil is
returned.
(and A B) will
return NIL.
or It takes any number of arguments. The arguments are
evaluated left to right until one evaluates to non-nil, in such
case the argument value is returned, otherwise it returns nil
(or A B) will
return 5
not It takes one argument and returns t if the
argument evaluates to nil.
(not A) will
return T.
27. BITWISE OPERATIONS ON NUMBERS
• Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for
bitwise and, or, and xor operations are as follows
p q P and q P or q P xor q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
28. DECISION MAKING
• Decision making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.
EXAMPLE
IF-ELSE STRUCTURE
30. LISP LOOP
• There may be a situation, when you need to execute a block of code numbers of times.
A loop statement allows us to execute a statement or group of statements multiple
times and following is the general form of a loop statement in most of the programming
languages.
• LISP provides the following types of constructs to handle looping requirements.
32. LISP LOOP
CONSTRUC
T
DESCRIPTION
loop The loop construct is the simplest form of iteration provided by LISP. In its simplest form, it
allows you to execute some statement(s) repeatedly until it finds a return statement.
Loop for The loop for construct allows you to implement a for-loop like iteration as most common in
other languages.
do The do construct is also used for performing iteration using LISP. It provides a structured
form of iteration.
dotimes The dotimes construct allows looping for some fixed number of
iterations.
dolist The dolist construct allows iteration through each element of a list.
33. LISP ARRAY
• LISP allows you to define single or multiple-dimension arrays using the make-
array function. An array can store any LISP object as its elements.
• All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
• In LISP, an array element is specified by a sequence of non-negative integer
• indices. The length of the sequence must equal the rank of the array. Indexing starts
from zero.
• The number of dimensions of an array is called its rank.
35. HOW TO DECLARE AN ARRAY
• For example, to create an array with 10- cells, named my-array, we can write:
• (setf my-array(make array(10)))
• The aref function allows accessing the contents of the cells. It takes two arguments, the
name of the array and the index value.
• For example, to access the content of the tenth cell, we write:
(aref my-array 9)
Editor's Notes
Refers to a software application that runs only on a particular type of computer.
Programs that run on a variety of different types of computers are called machine independent.