0% found this document useful (0 votes)
73 views18 pages

Imperative, OO and Functional Languages: A "C" Program Is

The document discusses different programming paradigms like imperative, object-oriented, and functional programming. It then focuses on the Lisp programming language, explaining its syntax of symbolic expressions and lists, how expressions are evaluated, and functions for manipulating lists. Various Lisp concepts are demonstrated like recursion, pattern matching, and defining functions.
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)
73 views18 pages

Imperative, OO and Functional Languages: A "C" Program Is

The document discusses different programming paradigms like imperative, object-oriented, and functional programming. It then focuses on the Lisp programming language, explaining its syntax of symbolic expressions and lists, how expressions are evaluated, and functions for manipulating lists. Various Lisp concepts are demonstrated like recursion, pattern matching, and defining functions.
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/ 18

1

Imperative, OO and
Functional Languages
!
A C program is
"
a web of assignment statements, interconnected by
control constructs which describe the time sequence
in which they are to be executed.
!
In Java programming,
"
objects are sent messages.
!
In pure LISP there is only
"
the evaluation of an expression by function application
"
instead of executing a program, LISP evaluates a
symbolic expression (s-expr)
Dr. Henry H. Leitner
2
Why Learn LISP ???
!
"Lisp is worth learning for the profound
enlightenment experience you will have when
you finally get it; that experience will make you
a better programmer for the rest of your days,
even if you never actually use Lisp itself a lot.
- Eric Raymond, "How to Become a Hacker"
!
For examples of some companies that use LISP,
see http://www.paulgraham.com/apps.html
3
Where Does LISP Come From?
4
Introduction to LISP
!
LISP syntax: symbolic expressions
"
atoms
"
lists of atoms
"
lists of s-exprs
!
Grammar for symbolic expressions:
"
<s-expr> ::= <atom> | <list>
"
<list> ::= ( <s-expr> * )
!
(name-or-description-of-a-function
arg
1
arg
2
arg
n
)
5
Evaluation of S-EXPRs
!
Parentheses must be taken seriously!
!
Quoting inhibits evaluation (EVAL does the
opposite!)
!
NIL is both a list and an atom
!
Defining your own functions using DEFUN
"
area of a circle
!
Other useful functions: IF, +, *, =
!
Defining a recursive function: factorial
6
Internal Representation
!
LISP lists are stored as linked-lists of records
with CAR and CDR fields
!
Diagramming list structure:
car cdr
first rest
fum foo bar
(foo bar) (cons fum )
7
Other LISP Functions
!
Assignment (side-effects) using SETF, SET
!
Lisp Manipulation Functions
"
CAR, CDR, CONS, LIST, APPEND
!
Predicates
"
EQ, EQL, EQUAL, ATOM, LISTP, CONSP, NULL,
ZEROP, PLUSP, MINUSP, EVENP, ODDP,
NUMBERP, SYMBOLP, BOUNDP, >, <, <=, >=, =, /=
"
Define a function to recursively compute the
length of a list
8
Predicates
!
Summary of commonly confused
primitive predicates:
A (A) nil
atom
t nil t
lisp
nil t t
consp
nil t nil
null
nil nil t
symbolp
t nil t
9
Defining 2 More Functions
!
Swap the first and second elements of a list.
"
e.g., (swap '(A B C D)) (B A C D)
" (defun swap (lst)
(cons '(cadr lst)
(cons (car l) (cddr l))))
"
Note: this builds a new list with some structure
shared with L
!
Compute the next even integer following n
" (defun next-even (n)
(if (evenp n) n (1+ n)))
10
The Most General Conditional
(cond (test
1
s-expr
11
s-expr
12
s-expr
1n
1
)
(test
2
s-expr
21
s-expr
22
s-expr
2n
2
)


(test
k
s-expr
k1
s-expr
k2
s-expr
kn
k
) )
11
Recursion on List Structures
!
CAR and CDR take lists apart
!
the analog of subtract 1 (when doing induction
on a number) is taking the CDR of a list
!
Our version of the built-in MEMBER function:
"
(defun memb (element lis)
(cond ((null lis) nil)
((eq (car lis) element) lis)
(t (memb element (cdr lis)))))
!
Recursion in two directions function OCCURS
"
(defun occurs (element lis)
(cond
Example of Nested List
12
A
B
X
17
13
Nameless Functions via LAMBDA
!
LISP programs are conceived and written with a
mathematical rigor, based upon the formalisms of
recursive function theory and the lambda
calculus.
"
Consider y + (x * y) for the values 3 and 4
"
Clarify using the Lambda notation of Alonzo Church.
"
In LISP, we use a similar notation
!
(MAPCAR F L)
"
F is a function of one parameter
" L is a list (x
1
x
2
x
n
)
" Produces a list (y
1
y
2
y
n
) where y
i
= (F x
i
)
14
Procedural Abstraction
!
SUM-INTEGERS computes !
!
SUM-SQUARES computes !
!
Now make the function-of-n itself a third
parameter:
"
SUM-TERMS computes !
!
Consider now the infinite series
and define PI-TERM(n) to compute the above

!
8
=
1
1 * 3
+
1
5 * 7
+
1
9 * 11
+
n = rst
last
last
n = rst
n = rst
last
n
n
2
term-fn(n)
Integration by Summation
!
! f(a + n.dx + dx/2) . dx
!
dx . ! f(a + n.dx + dx/2)
f(x)
a
b
0 1 2 3 4 5
dx
(b-a)/dx -1
n = 0
(b-a)/dx -1
n = 0
16
Symbolic Pattern Matching
!
Another kind of search problem: in a linear list
of words (symbols, whatever) to discover
specified patterns.
"
Although LISP itself has no built-in pattern-
matching, its a good implementation language for
such a function.
"
(match pattern data) will return T or NIL
"
The pattern may contain wildcard variables such
as ? (stand for one symbol) and * (stands for a
sequence of 0 or more symbols)
17
Pattern Matching, part 2
!
Wildcard examples
"
A ? B matches A A B
but not A B
nor A B C
"
A * B matches A A B
and A B
and A X Y Z B
"
* X * Y matches any sequence containing
both X and Y in that order
Additional Wildcards
18
!
?variable matches a single atom, and
assigns that atom to variable
!
*variable matches a sequence of ! 0
atoms, and assigns a list of that
sequence to variable
!
Example from Doctor program:
"
(cond ((match '(I am worried *blah-blah) userInput)
(princ (append '(How long have you been worried)
blah-blah)))

You might also like