0% found this document useful (0 votes)
18 views25 pages

U3 L1 Introduction to LISP

Uploaded by

khushidaryani29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

U3 L1 Introduction to LISP

Uploaded by

khushidaryani29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to AI

By

Manoj Kumar Singh


Assistant Professor
Department of Computer Science & Engineering

1 Lecture by: Manoj Kumar Singh


Unit-3
AI Programming Languages
(Topics- Introduction to LISP)

2 Lecture by: Manoj Kumar Singh


Introduction to LISP
 The programming language LISP takes its name from List Processing.

 LISP was developed by John McCarthy, during 1956-58 and was


implemented during 1959-62.

 LISP has a number of dialects. However, with the development of


COMMON LISP in the 1980s and its acceptance by a large number of
system implementers and manufactures, it has become almost standard
for LISP users.

 LISP has been one of the most popular languages for AI applications.

3 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Features of Common LISP
 It is machine-independent
 It uses iterative design methodology, and easy extensibility.
 It allows updating the programs dynamically.
 It provides high level debugging.
 It provides advanced object-oriented programming.
 It provides a convenient macro system.
 It provides wide-ranging data types like, objects, structures, lists,
vectors, adjustable arrays, hash-tables, and symbols.
 It is expression-based.
 It provides an object-oriented condition system.
 It provides a complete I/O library.
 It provides extensive control structures.

4 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Applications Built in LISP
 Large successful applications built in Lisp.
 Emacs
 G2
 AutoCad
 Igor Engraver
 Yahoo Store

5 Lecture by: Manoj Kumar Singh


Introduction to LISP
 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.

6 Lecture by: Manoj Kumar Singh


Introduction to LISP
 The Lisp Executer
 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. The windows version emulates a
unix environment using MingW under windows. The installer takes
care of this and automatically adds clisp to the windows PATH
variable.

7 Lecture by: Manoj Kumar Singh


Introduction to LISP
 The Lisp Executer
 Make program file in notepad and save “hello.lisp”
 Hello.lisp
 ; print the Hello World
 (write-line "Hello World")
 ; print the statment
 (write-line "I am Learning LISP")

 To run a *.lisp or *.lsp file, simply use below command at command


prompt
 clisp hello.lisp

 Output
 C:\Users\manoj>clisp hello.lisp
 Hello World
 I am Learning LISP

8 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Lisp - Program Structure
 LISP expressions are called symbolic expressions or s-expressions.
The s-expressions are composed of three valid objects, atoms, lists
and strings.

 Any s-expression is a valid program.

 LISP programs run either on an interpreter or as compiled code.

 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.

9 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Example: A Simple Program
 Let us write an s-expression to find the sum of three numbers 7, 9 and
11. To do this, we can type at the interpreter prompt.
 Plus.lisp
 ; execuate sum of three numbers
 (Write (+ 7 9 11))
 Output->

10 Lecture by: Manoj Kumar Singh


Introduction to LISP
 LISP Uses Prefix Notation
 You might have noted that LISP uses prefix notation.
 In the above program the + symbol works as the function name for
the process of summation of the numbers.
 In prefix notation, operators are written before their operands.

 For example, the expression,


 a*(b+c)/d
 will be written as −
 ; prefix mode operation
 (/ (* a (+ b c) ) d)

11 Lecture by: Manoj Kumar Singh


Introduction to LISP
 LISP Uses Prefix Notation
 For example, write code for converting Fahrenheit temp of 60o F to
the centigrade scale,
 (60 * 9 / 5) + 32
 temp.lisp
 ; evaluate and print arithmetic expression
 (write(+ (* (/ 9 5) 60) 32))
 Output->

12 Lecture by: Manoj Kumar Singh


Introduction to LISP
 LISP Basic Syntax
 LISP programs are made up of three basic building blocks
 atom
 list
 String
 An atom is a number or string of contiguous characters. It includes
numbers and special characters.
 Following are examples of some valid atoms
 name
 123008907
 *hello*
 Block#221
 abc123

13 Lecture by: Manoj Kumar Singh


Introduction to LISP
 LISP Basic Syntax
 A list is a sequence of atoms and/or other lists enclosed in
parentheses.
 Following are examples of 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)
 ()
 A string is a group of characters enclosed in double quotation marks.
 Following are examples of some valid strings
 " I am a string"
 "a ba c d efg #$%^&!"
 "Please enter the following details :"

14 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Adding comments in LISP
 The semicolon symbol (;) is used for indicating a comment line.
 For Example
 Comments.lisp
 (write-line "Hello World") ; greet the world
 ; tell them your whereabouts
 (write-line "I am Learning LISP")
 Output->

15 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Some important points in LISP
 The basic numeric operations in LISP are +, -, *, and /

 LISP represents a function call f(x) as (f x), for example cos(45) is


written as cos 45

 LISP expressions are case-insensitive, cos 45 or COS 45 are same.

 LISP tries to evaluate everything, including the arguments of a


function. Only three types of elements are constants and always
return their own value
 Numbers
 The letter t, that stands for logical true.
 The value nil, that stands for logical false, as well as an empty list.

16 Lecture by: Manoj Kumar Singh


Introduction to LISP
 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.

17 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Use of single quotation mark in LISP
 LISP evaluates everything including the function arguments and list
members.
 At times, we need to take atoms or lists literally and don't 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.
 The following example demonstrates this.
 Singlequotation.lisp
 (write-line "single quote used, it inhibits evaluation")
 (write '(* 2 3))
 (write-line " ")
 (write-line "single quote not used, so expression evaluated") (write (* 2 3))
 Output->

18 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Data Types in LISP
 In LISP, variables are not typed, but data objects are.
 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.

 Any variable can take any LISP object as its value, unless you have
declared it explicitly.

19 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Data Types in LISP
 Type Specifiers in LISP
 Type specifiers are system-defined symbols for data types.
array fixnum package simple-string

atom float pathname simple-vector

bignum function random-state single-float

bit hash-table ratio standard-char

bit-vector integer rational stream


character keyword readtable string

[common] list sequence [string-char]

compiled-function long-float short-float symbol

complex nill signed-byte t

cons null simple-array unsigned-byte

double-float number simple-bit-vector vector

20 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Example - Using scalar data types
 Scaler.lisp
 ; set values to variables
 (setq x 10)
 (setq y 34.567)
 (setq ch nil)
 (setq n 123.78)
 (setq bg 11.0e+4)
 (setq r 124/2)
 ; print values of variables
 (print x)
 (print y)
 (print n)
 (print ch)
 (print bg)
 (print r)
 Output->

21 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Example - Checking types of variables
 typesof.lisp
 ; define variables and set values
 (defvar x 10)
 (defvar y 34.567)
 (defvar ch nil)
 (defvar n 123.78)
 (defvar bg 11.0e+4)
 (defvar r 124/2)
 ; print type of all variables
 (print (type-of x))
 (print (type-of y))
 (print (type-of n))
 (print (type-of ch))
 (print (type-of bg))
 (print (type-of r))
22 Lecture by: Manoj Kumar Singh
 Output->
Introduction to LISP
 Lisp - Variables
 In LISP, each variable is represented by a symbol. The variable's
name is the name of the symbol and it is stored in the storage cell of
the symbol.
 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.
 Globalvar.lisp
 ; define a global variable x with a value of 234
 (defvar x 234)
 ; print value of x
 (write x)
 Output->

23 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Lisp – Assign Variables
 Since there is no type declaration for variables in LISP, you directly
specify a value for a symbol with the setq construct.

 Ex:
 ; set value of x as 10
 (setq x 10)
 (write x)

 Output->

24 Lecture by: Manoj Kumar Singh


Introduction to LISP
 Lisp – Global Variables
 Global2.lisp
 ; set x as 10
 (setq x 10)
 ; set y as 20
 (setq y 20)
 ; print x and y
 (format t "x = ~2d y = ~2d ~%" x y)
 ; update value of x as 100
 (setq x 100)
 ; update value of y as 200
 (setq y 200)
 ; print updated values of x and y
 (format t "x = ~2d y = ~2d" x y)
 Output->
25 Lecture by: Manoj Kumar Singh

You might also like