Unt 5-Functional Programming
Unt 5-Functional Programming
FUNCTIONAL
PROGRAMMING
Functional Programming
● Functional programming languages are specially designed to
handle symbolic computation and list processing
applications.
● Based on mathematical functions
● Some of the popular functional programming languages
include: Lisp, Python, Erlang, Haskell, Clojure, javascript
etc.
Functional Programming
Divided into 2 Groups
The strict means, evaluate the expression now and the lazy means, evaluate it for the first
use.
1.Strict evaluation
val s = factorial(15) / factorial(11)
println(s)
2.Lazy evaluation
lazy val l = factorial(15) / factorial(11)
println(l)
Functional Programming OOP
Focus is on: “What you are doing” Focus is on “How you are doing”
Functional Programming vs. Its methods can produce serious side effects.
Its functions have no-side effects
Object-oriented Programming
Flow Control is done using function calls & Flow control is done using loops and
function calls with recursion conditional statements.
Quotient
-(8)
MAximum
-(-8)
Minimum
Absolute
Add 1
Subtract 1
More LISP functions
(A B) (C)
Also note that when CDR is applied to a list with only one element,
it returns the empty list, sometimes denoted by 0.
CAR and CDR Operation of LISP
● CAR and CDR operations can be nested together
● (CAR (CDR (A B C))
● How far should the evaluation process go into an s-
expression?
● The user specifies where to stop evaluation by supplying an
● Often they are used on lists that have lists as elements, but
●(equal (+ 2 2 ) 4)
●output: T
●Some of the Predicates are : ATOM, BOUNDP, EQUAL, NULL,
NUMBERP, GREATERP and LESSP, ZEROP AND, OR, and NOT are
used to do Logic
Predicate
Sr.No. Predicate & Description
1
atom - It takes one argument and returns t if the argument is an atom or nil if otherwise.
2 equal - It takes two arguments and returns t if they are structurally equal or nil otherwise.
3 eq - It takes two arguments and returns t if they are same identical objects, sharing the same memory location or
nil otherwise.
4 eql - It takes two arguments and returns t if the arguments are eq, or if they are numbers of the same type with the
same value, or if they are character objects that represent the same character, or nil otherwise
5 evenp - It takes one numeric argument and returns t if the argument is even number or nil if otherwise.
6 oddp - It takes one numeric argument and returns t if the argument is odd number or nil if otherwise
7 zerop - It takes one numeric argument and returns t if the argument is zero or nil if otherwise.
8 null - It takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil.
ATOM
● ATOM is a predicate that tests its argument to see
if it is an atom.
● let us make the value of the atom DIGITS be a list of
the names of the numbers from zero to nine:
● (SETQ DIGITS '(ZERO ONE TWO THREE FOUR FIVE
SIX SEVEN EIGHT NINE))
ATOM
● (ATOM 'DIGITS)
○ T means
True
● (ATOM 'FIVE)
○ T
● (ATOM DIGITS)
○ NIL
● (ATOM '(ZERO ONE TWO THREE FOUR FIVE SIX SEVEN
EIGHT NINE))
○
BOUNDP
● BOUNDP expects a symbolic atom as its argument. It
tests if its argument has a value.
● If it does, it returns T. Otherwise it returns NIL.
● (BOUNDP 'DIGITS)
○ T
● (BOUNDP 'ZERO)
○ NIL
● (BOUNDP (CAR DIGITS))
○ NIL
EQUAL
● EQUAL is another fundamental predicate. It takes two
arguments and returns T if they are the same.
Otherwise it returns NIL:
● (EQUAL DIGITS DIGITS)
○ T
● (EQUAL 'DIGITS 'DIGITS)
○ T
● (EQUAL DIGITS '(ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE))
○ T
● (EQUAL DIGITS 'DIGITS)
○ NIL
NULL
● NULL checks to see if its argument is an empty list:
● (NULL '())
○ T
● (NULL T)
○ NIL
● (NULL DIGITS)
○ NIL
● (NULL 'DIGITS)
○ NIL
NUMBERP
● NUMBERP tests its argument to see if it is a number:
● (NUMBERP 3.14)
○ T
● (NUMBERP FIVE)
○ T
● (NUMBERP 'FIVE)
○ NIL
● (NUMBERP DIGITS)
○ NIL
● (NUMBERP 'DIGITS)
○ NIL
GREATERP and LESSP
● GREATERP and LESSP expect their arguments to be
numbers.
● GREATERP tests them to see that they are in strictly
descending order.
● LESSP checks to see that they are in strictly ascending
order.
● Both may be given any number of arguments.
GREATERP and LESSP
● (GREATERP FIVE 2)
○ T
● (GREATERP 2 FIVE)
○ NIL
● (LESSP 2 FIVE)
○ T
● (LESSP FIVE 2)
○ NIL
● (LESSP 2 2)
○ NIL
GREATERP and LESSP
● (GREATERP FIVE FOUR THREE TWO ONE)
○ T
● (GREATERP THREE ONE FOUR)
○ NIL
● (GREATERP 3 1 4)
○ NIL
ZEROP
● ZEROP expects a number. It tests its argument to see if it is
zero:
● (ZEROP ZERO)
○ T
● (ZEROP 'ZERO)
○ ERROR
● (ZEROP FIVE)
○ NIL
MINUSP
● MINUSP tests whether a number is negative:
● (MINUSP ONE)
○ NIL
● (MINUSP (MINUS ONE))
○ T
● (MINUSP ZERO)
○ NIL
NOT
● NOT returns T only if its argument is NIL.
● (NOT NIL)
○ T
● (NOT T)
○ NIL
● (NOT 'DOG)
○ NIL
● (SETQ PETS '(DOG CAT))
○ (DOG CAT)
● (NOT (MEMBER 'DOG PETS))
○ NIL
● (NOT (MEMBER 'TIGER PETS))
○ T
AND, OR and NOT
AND, OR and NOT
;set value 1 to 50
; set value 2 to 50 Output:
(setq val1 50)
50
(setq val2 50)
50
;and operator NIL
(print (and val1 val2)) NIL
;or operator
(print (or val1 val2))
(terpri) T
NIL
(write (evenp 10)) T
(terpri) NIL
T
(write (evenp 7 )) NIL
(terpri) NIL
NIL
(write (oddp 7 )) T
(terpri)
(write (zerop 0.0000000001))
(terpri)
(write (eq 3 3.0 ))
(terpri)
(write (equal 3 3.0 ))
(terpri)
(write (null nil ))
COND Select a Value among
Alternatives
(COND
(condition1 action1 )
(condition2 action2 )
...
(conditionN actionN ) )
COND Select a Value among Alternatives
● If no successful clause is found, COND returns NIL.
● If the successful clause consists of only one element, then
the value of that element itself is returned.
(cond
((= n 1) "N equals 1")
(t "N doesn't equal 1")
)
Some facts about LISP
● LISP is Neither Call-by-reference nor Call-by-value
● The objects are s-expressions.
● Free-variable Values are Determined Dynamically, not
Lexically
● Function Names can be Used as Arguments
RECURSION AND ITERATION
● How a function can use itself in its own definition is called recursion.
● How a function can do something repeatedly without using recursion.
This is called iteration.
● A control structure is a general scheme by which a program can go about
getting things done.
● Recursion and iteration are examples of control structures.
LAMBDA Defines Anonymous Functions