0% found this document useful (0 votes)
76 views15 pages

Lecture 18 LISP

This document discusses the Lisp programming language. It notes that Lisp was invented in 1958 and uses simple data structures of atoms and lists. It is an interpretive, functional programming language often used for AI. Valid data types in Lisp include numbers, symbols, strings, and lists. Functions are called using prefix notation. Evaluation involves evaluating each element of a list from left to right. Common functions include math functions and list constructors like cons, list, and append.

Uploaded by

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

Lecture 18 LISP

This document discusses the Lisp programming language. It notes that Lisp was invented in 1958 and uses simple data structures of atoms and lists. It is an interpretive, functional programming language often used for AI. Valid data types in Lisp include numbers, symbols, strings, and lists. Functions are called using prefix notation. Evaluation involves evaluating each element of a list from left to right. Common functions include math functions and list constructors like cons, list, and append.

Uploaded by

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

Modern

Programming
Languages
Lecture 18
Fakhar Lodhi
LISP
• Invented by John McCarthy (1958)
• Two simple data structure (atoms and lists)
• Heavy use of recursion
• Interpretive language
• Variations
– Scheme
– Common Lisp (de facto industrial standard)
• Most widely used AI programming language
• Functional Programming Paradigm
• Low maintenance overhead
Valid Objects
• Atoms may be;
• Numbers: (real 1.0, integer 1)
• Symbols: a consecutive sequence of characters (no space)
e.g. a, x, price-of-beef

• 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"
Valid Objects
• Lists: A list of atoms and/or lists, bounded by ( and ). For
example
 (a b c)
 (a (b c))

• Top elements of a list – first level


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’ stands for empty list, same as ()
Function Calls
• Its also a list
• Uses prefix notation:

(function-name arg1,arg2 ... argN)

• It returns function value for the given list of arguments


• Functions are either provided by LISP function library or
defined by the user
Function Calls
• Examples:

> (+ 1 3 5)
9

> (/ 3 5)
3/5

>Command
(/ 3.0 5)Line Prompt
0.59999999999999998

> (sqrt 4)
2
Evaluation of S-expression
1) Evaluating an atom
• Numerical and string atoms evaluate to themselves
• Symbols evaluate to their values if they are assigned
values, otherwise return Error
• The values of T and NIL are themselves
Evaluation of S-expression
2) Evaluate a list - evaluate every top element of the list
as follows, unless explicitly forbidden:
• The first element is always a function name;
evaluating it means to call the function body
• Each of the rest elements will then be evaluated, and
their values returned as the arguments for the
function
Evaluation of S-expression
• Examples :
3/5+4
> (+ (/ 3 5) 4)
23/5

> (+ (sqrt 4)
4.0)
6.0
> (sqrt x)
Error: The variable
X is unbound
Evaluation of S-expression
3) To assign a value to a symbol use: setq, set, setf

> (setq x 3.0)


3.0
>x
3.0

•‘setq’ is a special form of function (with two arguments)


 The first argument is a symbol which will not be evaluated
 The second argument is a S-expression, which will be
evaluated
 The value of the second argument is assigned to be the
value of the first argument
> (setq y x)
3.0

>y
3.0

> (+ x y)
6.0
• To forbid evaluation of a symbol use: quote or ’

> (quote
x)
x
> 'x
x

> (setq z
'x)
x
Functions
• Math functions
– +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min

L is a list containing elements


• Functions for creating lists or ‘List Constructors’
(A,B,C)
X is inserted at the
– Cons, list and append front/top of the list

> (cons 'x L) ; insert symbol x at the front of list L


(X A B C)

> (list 'a 'b 'c) ; making a list with the arguments as its elements
(A B C) ; if a, b, c have values A, B, C, then (list a b c)
; returns list (A B C)

> (append '(a b) '(c d)) ; appends one list in front of another
(A B C D)
Functions (cont’)
• Selectors
– first, rest, nth, car, cdr…
> (first '(a s d f)) ;returns the first element of a list
a

> (rest '((a s) d f)) ;deletes the first element from the
;list and returns the rest as a list
(d f)

> (first '((a s) d f))


(a s)

> (rest '((a s) (d f))


((d f))
> (setq L '(A B C))
(A B C)

> (car L)
; returns the first top level element of list L
A

> (cdr L)
; returns the rest of list L
(B C)

You might also like