Title: A functional program: Collection of functions
1Functional Programming
- A functional program Collection of functions
- A function just computes and returns a value
- No side-effects
- In fact No program variables whose values
change! - A function body Mainly calls to other functions
- Languages LISP, Scheme, ML, Haskell, ...
2Logic Programming
- Program just says what is needed, not how to
compute it - The system figures out the how
- DB systems are (sort of) logic programming
systems - Languages Prolog
- Logic programming is not very popular except as
DBs
3Functional Languages (Chap. 10)
- Must provide
- Suitable data types
- Set of primitive functions
- A notation for calling functions
- Way to construct new functions by composing
existing ones in different ways
4Lisp/Scheme Data Types
- Data Types Atomic and non-atomic S-expressions
(symbolic exps.) - Atoms
- Numbers (we use only integers)
- Strings xyz, 34 etc.
- Symbols (i.e., identifiers) XYZ, AB12,
etc.Some important symbols t (denotes
true written T in Lisp) f (denotes false
written NIL in Lisp)
5Lisp/Scheme Data Types (contd.)
- Non-atomic S-expressions If s1 and s2 are
s-expressions, so is (s1 . s2) - Important primitive functions
- cons s1, s2 (s1 . s2)
- car (s1 . s2) s1
- cdr (s1 . s2) s2
- cadr s car cdr s cadar s car
cdr car s - Important Everything in LISP/Scheme is an
s-exp. - Important Best to think in terms of how s-exps
are stored internally binary trees (using
pointers)
6List Notation
- List Notation
- ( ) denotes NIL
- (6) denotes (6 . NIL)
- (6 5) denotes (6 . (5 . NIL))
- (6 5 5) denotes (6 . (5 . (5 . NIL)))
- ((1 . 2) 3) denotes ((1 . 2) . (3 . NIL))
- ((1 . 2) (3 . 4)) denotes ((1 . 2) . ((3 . 4) .
NIL)) - In general
- (s1) denotes (s1 . NIL) s1 "dot version"
of s1 - (s1 s2) denotes (s1 . (s2 . NIL))
- (s1 s2 s3 ... ) denotes (s1 . (s2 . (s3 .
NIL))) etc.
7List Notation (contd.)
- The Scheme/LISP interpreter converts everything
to dot notation list notation is only for
input/output - Convert
- ((1 . 3) 2 4) ((1 . 3) (2 . 4)) ((1 . 3) (2
4)) - (1 . (2 . NIL)) ((1 . NIL) . (2 . NIL)) ((1 . 2)
. NIL) (1 . (2 . 3)) ((1 3) (2 4)) - Consider car, cdr, cddr etc. of ((1 . 2) (3 .
4)), ((1 . 2) . (3 . 4)) etc. - car, cdr, cons are best understood in terms of
how they manipulate s-expressions internally.
8Built-in Functions
- More functions
- eq? x, y returns t or f if x, y are/are
not same atomeq? t, f feq? f, f
teq? f, 5 f arguments to eq?
must be atoms - pair? x returns t if x is a "pair" f
otherwisepair? (2 . 3) tpair? (2 . t)
tpair? t fpair? () f - null? x returns t if x is () f
otherwise
9Built-in Functions (contd.)
- Standard math functions (arguments must be
numbers) - 10, -5 5
- - 10, -5 15
- 10, -5 -50
- / 10, 5 2 / 10, 7 10/7
- gt 10, -5 t
- 10, -5 f
- ... many others but we won't use most of them
- Important We have not yet seen any Scheme
programs - The above are just meanings of these built-in
functions
10Atoms, Parameters, Arguments
- Important Atoms are used for three purposes
- Constants numbers, t, f (we won't use
string consts.) - Function names car, cdr, eq?, , gt, ...
- Function parameters (in function definitions)
- Important Distinction between parameters and
arguments(also "formal parameters" and "actual
arguments")
11Defining New Functions
- addUpList L return sum of nos. in L (a list
of nos.) - addUpList L "design notation", not
Lisp/Scheme - null? L ? 0
- t ? carL, addUpList cdrL
- addUpList (2 3 4) returns 9 how does it
work? - nNil n if n is 4 return (NIL NIL NIL NIL)
- nNil n
- n, 0 ? NIL
- t ? cons NIL, nNIL -n, 1
- doubleUp s cons s, s what does it
do? - length L should return length of list L
- mysteryL nNil length L what does
it do?
12Scheme/Lisp "Programs"
- Starting Scheme (on stdsun)
- scheme48
- ... welcome, ...
- Type ,? for help
- type in a Scheme expression the interpreter
evaluates - it, outputs the value, waits for the next
Scheme exp. - gt ,exit
- Simplest Scheme expression constant atoms
- scheme48
- gt 655
- 655
-
13Scheme/Lisp Expressions
- Function application Fa1, a2, a3, ..., an ?
(F a1 a2 a3 ... an)The interpreter
evaluates a1, a2, ..., an binds the
resulting values to p1, p2, ..., pn, the pars of
Fthen it evaluates the body of F (as a Scheme
exp) - gt ( ( 2 3) ( 5 6) )
- evaluates ( 2 3), ( 5 6), binds to pars of
, then evaluates body of using values
bound to pars when needed - gt (cons ( 2 3) ( 5 6) ) similar
result - (5 . 30)
- gt (cons A B) error! "unbound A, B"
- gt (cons t f) okay! t, f evaluate to
t, f
14Quoted expressions
- gt (quote A) Don't evaluate A
- A
- (quote ...) looks like a function call but it
is not it can't be! - It is a form also "special form" only three
special forms - gt (cons (cdr (A . B) ) (car (A . B) ) )
Error! - gt (cons (cdr (quote (A . B)) ) (car (quote
(A . B)) ) ) - (B . A)
- gt (cons (quote (cdr (A . B)) ) (quote (car (A
. B)) ) ) ???
15Conditional expressions (Another Special Form)
- (cond (b1 e1) (b2 e2) ... (bn en) )
each bi, ei is a Scheme/Lisp expression - To evaluateEvaluate b1 if value is t,
evaluate e1, return that value - if b1 value is f, evaluate b2 if t, eval e2,
return that val - if b2 value is f, evaluate b3 if t, eval e3,
return that val - ...
- if b(n-1) value is f, eval bn if t, eval en,
return that val - else ... error!
- (cond
- ( (gt 5 3) 3 )
- ( t (3 . 5) ) )
- (cond
- ( (gt 3 5) (cons 3 5) )
- ( t (cons 5 3) ) )
16Function Definitions (Sp. Form)
- (define (F p1 p2 ... pn) ..body (Scheme
exp.).. ) - gt (define (silly p1 p2) 5)
- ..okay.. or some such acknowledgment
- gt (silly 10 20)5
- gt (silly (10 . 20) (20 . 30)) Error!
- gt (silly (cons 10 20) (cons 20 30)) 5
- gt (silly (quote (10 . 20)) (cons 20 30)) 5
17Defining new functions (contd)
- xmembx, list is x a member of list?
- null?list ? f
- eq?x, carlist ? t
- t ? xmembx, cdrlist
- gt (define (xmemb x list) is x a member of
list? - (cond
- ( (null? list) f )
- ( (eq? x (car list)) t ) quote
list? - ( t (xmemb x (cdr list) ) ) ) )
- no values returned
- gt (xmemb 3 (quote (2 3 4)) )
- t
18Function Definitions (contd.)
- equal x, y x and y may not be atoms
- pair?x ? pair?y ?
- equalcarx,cary ?
equalcdrx,cdry - t ? f
- t ? f
- pair?y ? f why?
- t ? eq?x,y
- gt (define (equal x y)
- (cond ( (pair? x)
- (cond ... ) )
- ( (pair? y) f )
- ( t (eq? x y)) ) )
- gt (define (atom? x) ...) ???
-
19Defining new functions (contd)
- xunions1, s2 union of atomic lists, less
duplicates - null?s1 --gt s2
- null?s2 --gt s1
- t --gt xmembcars1,s2 --gt xunioncdrs1,
s2 - t --gt cons cars1, xunioncdrs1,
s2 - better
- xunions1, s2
- null?s1 --gt s2
- null?s2 --gt s1
- xmembcars1,s2 --gt xunioncdrs1, s2
- t --gt cons cars1, xunioncdrs1, s2
20Function Definitions (contd.)
- addUpList L
- null? L ? 0
- t ? carL, addUpList cdrL
- gt (define (addUpList L)
- (cond
- ( (null? L) 0 )
- ( t ( (car L) (addUpList (cdr L) ) )
) ) ) - .. okay ..
- gt (addUpList (2 3 4) )
- Error!
- gt (addUpList (quote (2 3 4) ) )
- 9 but how does it work?
21Function Definitions (contd.)
- nNil n
- n, 0 ? NIL
- t ? cons NIL, nNIL -n, 1
- gt (define (nNil n)
- (cond
- (( n 0) (quote ()) ) why?
- (t (cons '() (nNil (- n 1))) ) ) )
- doubleUp s cons s, s
- (define (dUp s) (cons s s) ) need to
quote s?
22Different styles in Lisp
- maxListL returns max of number in
non-empty list L - Functional
- null?cdrL --gt carL
- gtcarL, maxListcdrL --gt carL
- t --gt maxListcdrL
- Functional but better
- null?cdrL --gt carL
- t --gt bigger carL, maxListcdrL
define bigger - imperative
- maxListL max2carL, cdrL
- max2x, L
- null?L --gt x
- gtx, carL --gt max2x, cdrL
- t --gt max2 carL, cdrL
23More functions
- How do you obtain first element of a list?
- How do you obtain the last element? watch out!
- How do you append two lists? No, not just
cons! - How do you reverse a list? best use imperative
trick
24How Lisp interpreter works
- Five types of Lisp expressions ("programs")
- Constants 4, 5, t etc. Evaluate to
themselves 4, 5 etc - Symbols X, Y, etc. Look them up on the
"association" list a-list. - Function application (F a1 a2 a3 ...
an) F is a (built-in or user-defined) function
that expects n parameters each ai is a Lisp
expression Evaluate each ai bind that value
aiv to pi, the corr. parameter, by
adding (pi . aiv) to the a-list then evaluate
the body of F
25Lisp Exps./How they are eval'd (contd)
- Quoted exp (QUOTE s) where s is any
s-exp evaluates to s - Conditional (COND (b1 e1) (b2 e2) ... (bn en) )
b1, b2, ... and e1, e2, ... are all Lisp
expressions eval. b1, b2, ... to find the
first bj that eval's to non-NIL eval corr. ej
return its value if all bi's eval to NIL,
error! - Fn. def. (DEFINE (F p1 ... pn) fe) F is new
fn., p1, ..., pn its parameters, fe the body
save def. on "d-list".
26Lisp Interpreter (partial) (in Lisp!)
- Three key functions
- eval evaluates a Lisp-exp.
- evcon evaluates a conditional Lisp-exp.
- apply applies a function to given set of
arguments - interpreterexp, dList evalexp, NIL, dList
why? - evconpairs, aList, dList
- null?pairs --gt "error"!
- evalcaarpairs,aList,dList --gt
evalcadarpairs,aList,dList - t --gt evconcdrpairs, aList, dList
27Lisp Interpreter (partial) (contd.)
- eval exp, aList, dList
- atom?exp --gt int?exp --gt exp
- eq?exp,t --gt t
- eq?exp,f --gt f
- in?exp,aList --gtgetValexp,aList
- t --gt "unbound variable!"
- atom?carexp --gt
- eq?carexp,QUOTE --gt cadrexp
- eq?carexp,COND --gt
- evconcdrexp, aList, dList
- t --gt applycarexp,
- evliscdrexp,aList,dList,
- aList, dList
- t --gt "error!"
28Scheme vs. LISP
- Differences (with LISP)
- t and f for "true" and "false" Lisp uses T,
NIL for this - NIL always written as () and is not a normal
atom In fact, Scheme talks of "pairs" vs.
"non-pairs", not "atoms" and "non-atoms" - Scope rule
- What "scope rule" means
- How it works in Lisp/Scheme
- Elimination of some imperative features