“The greatest single programming language ever designed.”— Alan Kay, on Lisp
“One of the most important and fascinating of
all computer languages is Lisp (standing for
"List Processing"), which was invented by
John McCarthy around the time Algol was
invented.”
- John McCarthy
BEGINNER LEVEL
• Lisp Introduction.
• Why & why not Lisp.
• Applications built using lisp.
• Building blocks in lisp.
• Syntax and Evaluation rules.
• Adding comments in lisp.
• Defining Functions.
• Lisp Primitives.
• List Operations using car, cdr,
append.
• Input output operations.
• Conditional and loops.
INTERMEDIATE LEVEL
• Data Structures.
• Dotted Pair.
• Lambda function.
• A LISt Processing language
– The basic data structure is linked list and Atoms.
• A functional programming language
– Each expression in LISP is a function that returns a value
• An interpretive language
– Running LISP programs involves interacting with the LISP interpreter.
– Clisp is the common lisp interpreter available only for Linux.
– Recently Compilers have been made for this language but they are not used a
lot.
•Lists (or S-expressions) are important: Why?
•Functions are defined as lists
•Lists can be manipulated easily in Lisp
•Functions can be manipulated easily
Why Lisp ?
• Because it’s the most widely used AI programming language
• Because AI researchers and theoreticians like using it
• Because it’s good for writing production software (Graham
article)
• Because it’s got lots of features other languages don’t
• Because you can write new programs and extend old
programs really, really quickly in Lisp
Why Not Lisp?
• Not uniformly object oriented.
• Not portable to other platforms like java.
• Unimportant features added by the Lisp
Machine Project at MIT which are difficult to
implement without the support of special
hardware.
• Too many concepts difficult to understand and
remember like (defparameter & devar).
APPLICATIONS BUILT USING LISP
BUILDING BLOCKS IN LISP
1. Valid objects (S-expressions)
ATOMS:
Numbers: (real 1.0, integer 1)
Symbols: a consecutive sequence of characters (no space)
E.g., a, x, price-of-bread.
Two special symbols: T and NIL for logical true and false.
STRINGS: a sequence of characters bounded by double quotes
E.g., "this is red".
LIST: a list of atoms and/or lists, bounded by "(" and ")“,
e.g., (a b c), (a (b c))
• Top elements of a list
example: top elements of list (a b c) are a, b, and c
top elements of list (a (b c)) are a and (b c)
• NIL: empty list, same as ().
(Note: LISP is case insensitive)
Atoms:
– Number
• examples: 3, 8.9, etc.
– Symbol
• An object written as a sequence of characters
• Symbols are usually manipulated as names that are “bound” to
other lisp objects
– Symbol FOO might be bound to 4.2
Atoms
Then how to get our own list in lisp??
nth element
A Special Symbol: NIL
• NIL represents an empty list.
• NIL is a terminator of a list.
• A list is usually built by inserting its elements
into NIL in the reverse order .
• NIL can also represent “false''.
• The special symbol representing “true” is T.
Basic Evaluation Rules
• A number evaluates to itself
• A symbol evaluates to its value.
• A list is evaluated by
– treating the first element as a function
– evaluating each arguments of the function in a left-to-right order
• An expression preceded by a quote symbol ‘
evaluates to the expression itself.
ADDING COMMENTS IN LISP
Single line comment is added with ‘ ; ’
semicolon.
Eg. (write-line "Hello World") ; greet the world
output: Hello World
“Hello World”
MULTI-LINE COMMENTS
Multiline comments are added as below:
#|
Your Comments Here
....
|#
2.Functions
• Lisp is a functional language – So everything is a function
-> Functions in other languages
var sum = sqrt(x)
Print sum
In lisp this function is:
-> (sqrt x)
• Most LISP functions require you to think and use Prefix
Notation – you have to think backwards
– (print (+ 5 (sqrt 6)))
• And there are primary effects and side effects
– In ‘pure’ Lisp, we ONLY get values by executing a function.
– We DON’T set a variable to a new value INSIDE a
function….that is a side effect of executing a function. (You
all probably do that all the time in Java or C++)
Defining Functions
• A function is defined using DEFUN
• (DEFUN <fn-name> (<arg1> ...<argK>)
<exp1> ... <expN> )
• All arguments are passed by value.
• The body of the function may contain any
number of expressions (i.e., function calls).
Defining A Function
(defun square (x)
(* x x) )
(defun add-friend (new-friend friends)
(cons new-friend friends) )
How the Lisp Functions work – The
read-eval loop
• Evaluation process starts with an “S”
expression (i.e., a function and operands to
that function) This one adds two numbers….
(+ 3 4)
Parentheses – Notification to evaluate
Function name – Go get function (in this case ‘+’ is
the add function)
space - separator
operands -- value for function
Parentheses – stop evaluation and return the
answer
Arithmetic Functions
• + addition - any number of arguments; with no arguments
gives the identity element for addition, 0.
• - negation - one argument; subtraction - two or more
arguments; illegal with no arguments.
• * multiplication - any number of arguments; with no
arguments gives the identity element for multiplication, 1.
• / division - two or more arguments; illegal with no
arguments.
• 1+ increment by 1 - one argument only.
• 1- decrement by 1 - one argument only.
• exp e to the power - one argument only.
• expt number to the power - two arguments only.
• setf more
general than
setq
• binding
setq & let
First & Rest
• In particular, FIRST and REST are non-
destructive.
> (setq my-friends ‘(Superman Batman Robin) )
(Superman Batman Robin)
> (first (rest my-friends))
Batman
> my-friends
(Superman Batman Robin)
List Operations cons, car, cdr, append
• The cons function constructs lists; it is the inverse
of car and cdr. For example, cons can be used to make a four
element list from the three element list, (fir oak maple):
• (cons 'pine '(fir oak maple)) After evaluating this list, you will
see
(pine fir oak maple)
(car '(pine fir oak maple))
(cdr '(pine fir oak maple))
• car and cdr are applied to a list made up of symbols, such as
the list (pine fir oak maple), the element of the list returned by
the function car is the symbol pine without any parentheses
around it. pine is the first element in the list.
• However, the cdr of the list is a list itself, (fir oak maple), as you
can see by evaluating the following expressions in the usual
way:
append
• append - any number of arguments - all, save
the last, must be true lists (and it usually is,
too). Combines its arguments into a single list,
which will be a true list if the last argument is.
For example,(append '(1 2) '(3 4)) is (1 2 3 4)
Input-Output operation
• Lisp provides a large number of input output operations. Input operations
are functions whose value is some input read from an external stream;
output operations are typically functions which have the side effect of
printing something on an external stream and also return as their function
value that which was printed.
• read no argument - reads a single s-expression
• print single argument - prints a single s-expression, preceeded by a
newline and followed by a single space.
• prin1 single argument – prin1 function sends a string without formatting.
• princ single argument - The princ function sends a string after formatting
any control characters.
• terpri no argument - prints a single newline.
CONDITIONALS
If you want an if else statement, then you'd
want to use the cond function.
‘cond()’ function
• cond is an unusual function which may take
any arbitrary number of arguments.
• (COND
(condition1 result1 )
(condition2 result2 )
. . .
(T resultN ) )
Terminates a COND with a T condition
(defun select-character (enemy)
(cond ( (equal enemy ‘Penguin)
‘Batman)
( (equal enemy ‘Catwoman)
‘J-Bond )
( (equal enemy ‘Black-Knight)
‘(White-Knight King-Arthur ) )
( T ; for all other enemies
‘SuperMan) ; ask Superman for help
)
)
Loops
Construct 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.
,
• (setq a 10)
Output : 10
(loop (setq a (+ a 1))
(write a)
(terpri)
(when (> a 17) (return a)))
Output : 11
12
...
18
LOOP
loop for
• (loop for loop-variable from
value1 to value2 do (action))
Example : (loop for a from 10 to 20
do (print a) )
Output:
10
11
12
...
20
NIL
‘do’ construct
(do (variable1 value1 updated-value1)
(variable2 value2 updated-value2)
(variable3 value3 updated-value3)
...
(test return-value)
(s-expressions))
Note After each iteration, the test is evaluated, and if it returns a non-nil or true, the
return-value is evaluated and returned.
(do ((x 0 (+ 2 x))
(y 20 ( - y 2)))
((= x y) (- x y))
(format t "~% x = ~d y = ~d" x y))
dotimes
• (dotimes (n 11)
(print n)
(print (* n n)))
Output:
0 0
1 1 ...
2 4
10 100
NIL
dolist
• (dolist (n '(1 2 3 4 5 6 7 8 9))
(format t "~% Number: ~d Square: ~d" n (* n
n)))
Output: Number: 1 Square 1
Number: 2 Square 4 ..
Number: 9 Square 81
>(reverse ‘(1 2 3 4))
>(4 3 2 1)
Intermediate Level
DEFINING STRUCTURES IN LISP
• Dotted
pairs
• Dotted pairs
NOTE: Here we defined a function ADDER which returns a function object.
Lisp

Lisp

  • 2.
    “The greatest singleprogramming language ever designed.”— Alan Kay, on Lisp “One of the most important and fascinating of all computer languages is Lisp (standing for "List Processing"), which was invented by John McCarthy around the time Algol was invented.” - John McCarthy
  • 3.
    BEGINNER LEVEL • LispIntroduction. • Why & why not Lisp. • Applications built using lisp. • Building blocks in lisp. • Syntax and Evaluation rules. • Adding comments in lisp. • Defining Functions. • Lisp Primitives. • List Operations using car, cdr, append. • Input output operations. • Conditional and loops. INTERMEDIATE LEVEL • Data Structures. • Dotted Pair. • Lambda function.
  • 4.
    • A LIStProcessing language – The basic data structure is linked list and Atoms. • A functional programming language – Each expression in LISP is a function that returns a value • An interpretive language – Running LISP programs involves interacting with the LISP interpreter. – Clisp is the common lisp interpreter available only for Linux. – Recently Compilers have been made for this language but they are not used a lot. •Lists (or S-expressions) are important: Why? •Functions are defined as lists •Lists can be manipulated easily in Lisp •Functions can be manipulated easily
  • 5.
    Why Lisp ? •Because it’s the most widely used AI programming language • Because AI researchers and theoreticians like using it • Because it’s good for writing production software (Graham article) • Because it’s got lots of features other languages don’t • Because you can write new programs and extend old programs really, really quickly in Lisp
  • 6.
    Why Not Lisp? •Not uniformly object oriented. • Not portable to other platforms like java. • Unimportant features added by the Lisp Machine Project at MIT which are difficult to implement without the support of special hardware. • Too many concepts difficult to understand and remember like (defparameter & devar).
  • 7.
  • 8.
    BUILDING BLOCKS INLISP 1. Valid objects (S-expressions) ATOMS: Numbers: (real 1.0, integer 1) Symbols: a consecutive sequence of characters (no space) E.g., a, x, price-of-bread. Two special symbols: T and NIL for logical true and false. STRINGS: a sequence of characters bounded by double quotes E.g., "this is red". LIST: a list of atoms and/or lists, bounded by "(" and ")“, e.g., (a b c), (a (b c)) • Top elements of a list example: top elements of list (a b c) are a, b, and c top elements of list (a (b c)) are a and (b c) • NIL: empty list, same as (). (Note: LISP is case insensitive)
  • 9.
    Atoms: – Number • examples:3, 8.9, etc. – Symbol • An object written as a sequence of characters • Symbols are usually manipulated as names that are “bound” to other lisp objects – Symbol FOO might be bound to 4.2 Atoms
  • 11.
    Then how toget our own list in lisp??
  • 13.
  • 14.
    A Special Symbol:NIL • NIL represents an empty list. • NIL is a terminator of a list. • A list is usually built by inserting its elements into NIL in the reverse order . • NIL can also represent “false''. • The special symbol representing “true” is T.
  • 16.
    Basic Evaluation Rules •A number evaluates to itself • A symbol evaluates to its value. • A list is evaluated by – treating the first element as a function – evaluating each arguments of the function in a left-to-right order • An expression preceded by a quote symbol ‘ evaluates to the expression itself.
  • 17.
    ADDING COMMENTS INLISP Single line comment is added with ‘ ; ’ semicolon. Eg. (write-line "Hello World") ; greet the world output: Hello World “Hello World”
  • 18.
    MULTI-LINE COMMENTS Multiline commentsare added as below: #| Your Comments Here .... |#
  • 19.
    2.Functions • Lisp isa functional language – So everything is a function -> Functions in other languages var sum = sqrt(x) Print sum In lisp this function is: -> (sqrt x) • Most LISP functions require you to think and use Prefix Notation – you have to think backwards – (print (+ 5 (sqrt 6))) • And there are primary effects and side effects – In ‘pure’ Lisp, we ONLY get values by executing a function. – We DON’T set a variable to a new value INSIDE a function….that is a side effect of executing a function. (You all probably do that all the time in Java or C++)
  • 20.
    Defining Functions • Afunction is defined using DEFUN • (DEFUN <fn-name> (<arg1> ...<argK>) <exp1> ... <expN> ) • All arguments are passed by value. • The body of the function may contain any number of expressions (i.e., function calls).
  • 21.
    Defining A Function (defunsquare (x) (* x x) ) (defun add-friend (new-friend friends) (cons new-friend friends) )
  • 22.
    How the LispFunctions work – The read-eval loop • Evaluation process starts with an “S” expression (i.e., a function and operands to that function) This one adds two numbers…. (+ 3 4) Parentheses – Notification to evaluate Function name – Go get function (in this case ‘+’ is the add function) space - separator operands -- value for function Parentheses – stop evaluation and return the answer
  • 24.
    Arithmetic Functions • +addition - any number of arguments; with no arguments gives the identity element for addition, 0. • - negation - one argument; subtraction - two or more arguments; illegal with no arguments. • * multiplication - any number of arguments; with no arguments gives the identity element for multiplication, 1. • / division - two or more arguments; illegal with no arguments. • 1+ increment by 1 - one argument only. • 1- decrement by 1 - one argument only. • exp e to the power - one argument only. • expt number to the power - two arguments only.
  • 26.
    • setf more generalthan setq • binding
  • 29.
  • 30.
    First & Rest •In particular, FIRST and REST are non- destructive. > (setq my-friends ‘(Superman Batman Robin) ) (Superman Batman Robin) > (first (rest my-friends)) Batman > my-friends (Superman Batman Robin)
  • 31.
    List Operations cons,car, cdr, append • The cons function constructs lists; it is the inverse of car and cdr. For example, cons can be used to make a four element list from the three element list, (fir oak maple): • (cons 'pine '(fir oak maple)) After evaluating this list, you will see (pine fir oak maple) (car '(pine fir oak maple)) (cdr '(pine fir oak maple)) • car and cdr are applied to a list made up of symbols, such as the list (pine fir oak maple), the element of the list returned by the function car is the symbol pine without any parentheses around it. pine is the first element in the list. • However, the cdr of the list is a list itself, (fir oak maple), as you can see by evaluating the following expressions in the usual way:
  • 33.
    append • append -any number of arguments - all, save the last, must be true lists (and it usually is, too). Combines its arguments into a single list, which will be a true list if the last argument is. For example,(append '(1 2) '(3 4)) is (1 2 3 4)
  • 34.
    Input-Output operation • Lispprovides a large number of input output operations. Input operations are functions whose value is some input read from an external stream; output operations are typically functions which have the side effect of printing something on an external stream and also return as their function value that which was printed. • read no argument - reads a single s-expression • print single argument - prints a single s-expression, preceeded by a newline and followed by a single space. • prin1 single argument – prin1 function sends a string without formatting. • princ single argument - The princ function sends a string after formatting any control characters. • terpri no argument - prints a single newline.
  • 36.
    CONDITIONALS If you wantan if else statement, then you'd want to use the cond function.
  • 37.
    ‘cond()’ function • condis an unusual function which may take any arbitrary number of arguments. • (COND (condition1 result1 ) (condition2 result2 ) . . . (T resultN ) )
  • 39.
    Terminates a CONDwith a T condition (defun select-character (enemy) (cond ( (equal enemy ‘Penguin) ‘Batman) ( (equal enemy ‘Catwoman) ‘J-Bond ) ( (equal enemy ‘Black-Knight) ‘(White-Knight King-Arthur ) ) ( T ; for all other enemies ‘SuperMan) ; ask Superman for help ) )
  • 40.
    Loops Construct Description loop Theloop 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.
  • 41.
    , • (setq a10) Output : 10 (loop (setq a (+ a 1)) (write a) (terpri) (when (> a 17) (return a))) Output : 11 12 ... 18 LOOP
  • 42.
    loop for • (loopfor loop-variable from value1 to value2 do (action)) Example : (loop for a from 10 to 20 do (print a) ) Output: 10 11 12 ... 20 NIL
  • 43.
    ‘do’ construct (do (variable1value1 updated-value1) (variable2 value2 updated-value2) (variable3 value3 updated-value3) ... (test return-value) (s-expressions)) Note After each iteration, the test is evaluated, and if it returns a non-nil or true, the return-value is evaluated and returned. (do ((x 0 (+ 2 x)) (y 20 ( - y 2))) ((= x y) (- x y)) (format t "~% x = ~d y = ~d" x y))
  • 45.
    dotimes • (dotimes (n11) (print n) (print (* n n))) Output: 0 0 1 1 ... 2 4 10 100 NIL
  • 46.
    dolist • (dolist (n'(1 2 3 4 5 6 7 8 9)) (format t "~% Number: ~d Square: ~d" n (* n n))) Output: Number: 1 Square 1 Number: 2 Square 4 .. Number: 9 Square 81
  • 47.
    >(reverse ‘(1 23 4)) >(4 3 2 1)
  • 48.
  • 50.
  • 51.
  • 52.
  • 56.
    NOTE: Here wedefined a function ADDER which returns a function object.