KEMBAR78
Report about the LISP Programming Language | PPTX
Prepared by Mel Andrew Maldos
The LISP Programming Language
Overview
LISP stands for List Programming.
Designed by John McCarthy
at Massachusetts Institute of Technology (MIT)
on 1958.
Language Category: High Level Language
Programming Domain: Artificial Intelligence
Overview
It was first implemented by Steve Russell on an IBM 704
computer.
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 serves as a common language, which can be easily
extended for specific implementation. Programs written in
Common LISP do not depend on machine-specific
characteristics, such as word length etc.
LISP was implemented first as an interpreted language , in
the early 60’s, on IBM 704 computers. However a few years later,
it was implemented as a compiled language. Still today LISP and
several other languages from the LISP family still carry this
mixed legacy, and can be interpreted or compile, or even
executed in a mixed environment.
Compiled or Interpreted
Valid Objects
LISP expressions are called symbolic expressions or S-
expressions. The S-expressions are composed of three valid
objects: Atoms, Lists and Strings.
An atom is a number or string of contiguous characters. It
includes numbers and special characters. The following
examples show some valid atoms:
• hello-from-tutorials-point
• name
• 123008907
Valid Objects
• *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)
• ( )
Valid Objects
A string is a group of characters enclosed in double
quotation marks. The following examples show some valid
strings:
• " I am a string"
• "a ba c d efg #$%^&!"
• "Please enter the following details:"
Data Types & Declaration
LISP data types can be categorized as:
• Scalar types - numbers, characters, symbols etc.
• Data structures - lists, vectors, bit-vectors, and
strings.
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.
Code:
(setq x 10)
Data Types & Declaration
Example:
Code:
(defvar x 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:
Data Types & Declaration
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.
Code:
(prog ((x '(a b c))
Data Types & Declaration
Example (let Construct):
Code:
(let (x ‘a))
Example (prog Construct):
Code:
;This is a comment
;This is another comment
Comments
The semicolon symbol (;) is used for indicating a
comment line.
Example:
Code:
(write-line "Hello World")
(prog ((x '(a b c))
(y '(1 2 3))
(z '(p q 10)))
(format t "x = ~a y = ~a z = ~a" x y z))
Delimiter
There is no delimiter for each statement in LISP but
statement must be enclosed within a parenthesis ().
Example:
Operations
Arithmetic
Operations
Comparison
Operations
Logical Operations on Boolean Values
Operations
Bitwise Operations on Numbers
LISP provides following types of decision making
constructs:
when - In simplest form it is followed by a test clause, and a test
action. If the test clause evaluates to true, then the test action is
executed. Otherwise, the consequent clause is evaluated.
case - This construct implements multiple test-action clauses
such as cond construct. However, it evaluates a key form and
allows multiple action clauses based on the evaluation of that
key form.
Logical Selection Statements
Logical Selection Statements
cond - This construct is used for checking multiple test-
action clauses. It can be compared to the nested if
statements in other programming languages.
if - The if construct has various forms. In simplest form it
is followed by a test clause, a test action and some other
consequent action(s). If the test clause evaluates to true,
then the test action is executed. Otherwise, the
consequent clause is evaluated.
Logical Selection Statements
Example (when Construct):
Code:
(setq a 100)
(when (> a 20)
(format t "~% a is
greater than 20"))
(format t "~% value of a
is ~d " a)
Output:
a is greater than 20
value of a is 100
Logical Selection Statements
Example (case Construct):
Code:
(setq day 6)
(case day
(1 (format t "~% Sunday"))
(2 (format t "~% Monday"))
(3 (format t "~% Tuesday"))
(4 (format t "~%
Wednesday"))
(5 (format t "~%
Thursday"))
(6 (format t "~% Friday"))
(7 (format t "~%
Saturday")))
Output:
Friday
Logical Selection Statements
Example (cond Construct):
Code:
(setq a 10)
(cond ((> a 20)
(format t "~% a is
less than 20"))
(t (format t "~% value of
a is ~d " a)))
Output:
value of a is 10
Logical Selection Statements
Example (cond Construct):
Code:
(setq a 10)
(if (> a 20)
(format t "~% a is
less than 20"))
(format t "~% value of a
is ~d " a)
Output:
value of a is 10
Loops
LISP provides the following types of constructs to
handle looping requirements.
Loop - It 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 - It allows you to implement a for-loop like
iteration as most common in other languages.
Loops
do - It is also used for performing iteration using LISP. It
provides a structured form of iteration.
dotimes- It allows looping for some fixed number of
iterations.
dolist - It allows iteration through each element of a list.
Loops
Example (Loop Construct):
Code:
(setq a 10)
(loop
(setq a (+ a 1))
(write a)
(terpri)
(when (> a 17) (return
a)))
Output:
11
12
13
14
15
16
17
18
Output:
0 0
1 1
2 4
Loops
Example (loop for Construct):
Code:
(loop for a from 10 to 15
do (print a)
)
Output:
10
11
12
13
14
15
Example (dotimes Construct):
Code:
(dotimes (n 3)
(print n) (print (* n
n)))
Loops
Example (do Construct):
Code:
(do ((x 0 (+ 2 x))
(y 20 ( - y 2)))
((= x y)(- x y))
(format t "~% x = ~d y =
~d" x y))
Output:
x = 0 y = 20
x = 2 y = 18
x = 4 y = 16
x = 6 y = 14
x = 8 y = 12
Loops
Output:
Number: 1 Square: 1
Number: 2 Square: 4
Number: 3 Square: 9
Number: 4 Square: 16
Number: 5 Square: 25
Number: 6 Square: 36
Number: 7 Square: 49
Number: 8 Square: 64
Example (dolist Construct):
Code:
(dolist (n '(1 2 3 4 5
6 7 8 9))
(format t "~% Number:
~d Square: ~d" n (* n
n)))

Report about the LISP Programming Language

  • 1.
    Prepared by MelAndrew Maldos The LISP Programming Language
  • 2.
    Overview LISP stands forList Programming. Designed by John McCarthy at Massachusetts Institute of Technology (MIT) on 1958. Language Category: High Level Language Programming Domain: Artificial Intelligence
  • 3.
    Overview It was firstimplemented by Steve Russell on an IBM 704 computer. 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 serves as a common language, which can be easily extended for specific implementation. Programs written in Common LISP do not depend on machine-specific characteristics, such as word length etc.
  • 4.
    LISP was implementedfirst as an interpreted language , in the early 60’s, on IBM 704 computers. However a few years later, it was implemented as a compiled language. Still today LISP and several other languages from the LISP family still carry this mixed legacy, and can be interpreted or compile, or even executed in a mixed environment. Compiled or Interpreted
  • 5.
    Valid Objects LISP expressionsare called symbolic expressions or S- expressions. The S-expressions are composed of three valid objects: Atoms, Lists and Strings. An atom is a number or string of contiguous characters. It includes numbers and special characters. The following examples show some valid atoms: • hello-from-tutorials-point • name • 123008907
  • 6.
    Valid Objects • *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) • ( )
  • 7.
    Valid Objects A stringis a group of characters enclosed in double quotation marks. The following examples show some valid strings: • " I am a string" • "a ba c d efg #$%^&!" • "Please enter the following details:"
  • 8.
    Data Types &Declaration LISP data types can be categorized as: • Scalar types - numbers, characters, symbols etc. • Data structures - lists, vectors, bit-vectors, and strings. 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.
  • 9.
    Code: (setq x 10) DataTypes & Declaration Example: Code: (defvar x 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:
  • 10.
    Data Types &Declaration 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.
  • 11.
    Code: (prog ((x '(ab c)) Data Types & Declaration Example (let Construct): Code: (let (x ‘a)) Example (prog Construct):
  • 12.
    Code: ;This is acomment ;This is another comment Comments The semicolon symbol (;) is used for indicating a comment line. Example:
  • 13.
    Code: (write-line "Hello World") (prog((x '(a b c)) (y '(1 2 3)) (z '(p q 10))) (format t "x = ~a y = ~a z = ~a" x y z)) Delimiter There is no delimiter for each statement in LISP but statement must be enclosed within a parenthesis (). Example:
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    LISP provides followingtypes of decision making constructs: when - In simplest form it is followed by a test clause, and a test action. If the test clause evaluates to true, then the test action is executed. Otherwise, the consequent clause is evaluated. case - This construct implements multiple test-action clauses such as cond construct. However, it evaluates a key form and allows multiple action clauses based on the evaluation of that key form. Logical Selection Statements
  • 19.
    Logical Selection Statements cond- This construct is used for checking multiple test- action clauses. It can be compared to the nested if statements in other programming languages. if - The if construct has various forms. In simplest form it is followed by a test clause, a test action and some other consequent action(s). If the test clause evaluates to true, then the test action is executed. Otherwise, the consequent clause is evaluated.
  • 20.
    Logical Selection Statements Example(when Construct): Code: (setq a 100) (when (> a 20) (format t "~% a is greater than 20")) (format t "~% value of a is ~d " a) Output: a is greater than 20 value of a is 100
  • 21.
    Logical Selection Statements Example(case Construct): Code: (setq day 6) (case day (1 (format t "~% Sunday")) (2 (format t "~% Monday")) (3 (format t "~% Tuesday")) (4 (format t "~% Wednesday")) (5 (format t "~% Thursday")) (6 (format t "~% Friday")) (7 (format t "~% Saturday"))) Output: Friday
  • 22.
    Logical Selection Statements Example(cond Construct): Code: (setq a 10) (cond ((> a 20) (format t "~% a is less than 20")) (t (format t "~% value of a is ~d " a))) Output: value of a is 10
  • 23.
    Logical Selection Statements Example(cond Construct): Code: (setq a 10) (if (> a 20) (format t "~% a is less than 20")) (format t "~% value of a is ~d " a) Output: value of a is 10
  • 24.
    Loops LISP provides thefollowing types of constructs to handle looping requirements. Loop - It 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 - It allows you to implement a for-loop like iteration as most common in other languages.
  • 25.
    Loops do - Itis also used for performing iteration using LISP. It provides a structured form of iteration. dotimes- It allows looping for some fixed number of iterations. dolist - It allows iteration through each element of a list.
  • 26.
    Loops Example (Loop Construct): Code: (setqa 10) (loop (setq a (+ a 1)) (write a) (terpri) (when (> a 17) (return a))) Output: 11 12 13 14 15 16 17 18
  • 27.
    Output: 0 0 1 1 24 Loops Example (loop for Construct): Code: (loop for a from 10 to 15 do (print a) ) Output: 10 11 12 13 14 15 Example (dotimes Construct): Code: (dotimes (n 3) (print n) (print (* n n)))
  • 28.
    Loops Example (do Construct): Code: (do((x 0 (+ 2 x)) (y 20 ( - y 2))) ((= x y)(- x y)) (format t "~% x = ~d y = ~d" x y)) Output: x = 0 y = 20 x = 2 y = 18 x = 4 y = 16 x = 6 y = 14 x = 8 y = 12
  • 29.
    Loops Output: Number: 1 Square:1 Number: 2 Square: 4 Number: 3 Square: 9 Number: 4 Square: 16 Number: 5 Square: 25 Number: 6 Square: 36 Number: 7 Square: 49 Number: 8 Square: 64 Example (dolist Construct): Code: (dolist (n '(1 2 3 4 5 6 7 8 9)) (format t "~% Number: ~d Square: ~d" n (* n n)))