You are on page 1of 45

USER DEFINED FUNCTIONS

Writing user defined functions or procedures

(DEFUN <Procedure name>


(<Parameter 1> <Parameter 2>…<Parameter n)
form 1
form 2
:
form n)

1
EXAMPLES OF USER DEFINED
FUNCTIONS
; Computes square of a number
(Defun square (x) (* x x))
(square 5) → 25
(square 10) → 100
; reverses a pair of elements
(defun exchange (pair)
(list (second pair)(first pair)))
(exchange ‘(a b)) → (b a)
2
EXAMPLES OF USER DEFINED
FUNCTIONS (Contd..)
; converting from Centigrade to
Fahrenheit temperature
(defun c-to-f ( c )
(+(*(/ 9 5) c) 32))

DEFUN doesn’t evaluate its arguments,


it establishes a procedure definition.

3
EXAMPLES OF USER DEFINED
FUNCTIONS (Contd..)
• The value that DEFUN gives back is the
procedure name.
• A new place in memory to hold a value
for the parameter is reserved.
• The process of preparing memory
space for parameter values is called
binding.
• Parameters are bound when
procedures are called.
4
LEXICAL VARIBLES AND
SPECIAL VARIBLES
• A variable is a lexical variable if it
appears inside a procedure in which it
is a parameter.
• It is also called a local variable.
• As parameters are assigned values
when the procedure is called, we can
say that Lexical variables are isolated
by a virtual fence.
5
SPECIAL VARIBLES
• A variable is a special variable or a
free variable or a global variable if
it appears inside a procedure in
which it is not a parameter.
• A virtual fence does not isolate it.

6
EXAMPLE OF LEXICAL
VARIBLE
; demonstration of the use of Lexical
variable
(setf clist ‘(Bombay Calcutta Delhi))
(defun fl (clist)
(cons (car clist) (last clist)))
(fl ‘(RAM SHYAM KARAN)) → (RAM KARAN)
as clist is bound to
(RAM SHYAM KARAN)
7
EXAMPLE OF FREE
VARIABLE
; demonstration of the use of free
variable
(setf clist ‘(RAM SHYAM KARAN))
(defun fl ( )
(setf clist ‘(Bombay Calcutta Delhi))
(setf clist (cons (first clist) (last
clist))))
(fl) (Bombay Delhi)
8
OPTIONAL PARAMETERS
For optional parameters, there may not
be corresponding arguments.
(root 9), (root 9 2), (root 27 3)
(defun root (x &optional n)
(if n
(expt x(/ 1 n))
(sqrt x)))

9
OPTIONAL PARAMETERS (Contd..)

When n is nil, the default value Root


uses sqrt.
(root 9) is same as (sqrt 9)
Default values also can be supplied
along with optional parameter(s).
(defun root (x &optional (n 2))
(expt x(/ 1 n)))

10
LET & LET* BIND PARAMETERS
TO INITIAL VALUES
General template for LET is
(let ((Var1 value-1)
(Var2 value-2
.
:
(Var n value-n))
form1
.
:
form n )

11
EXAMPLE OF LET
(defun average (x y)
(let ((sum (+ x y))
(list x y ‘average ’is (/ sum 2.0)))
(average 3 7)
(3 7 average is 5.0)

12
ANOTHER EXAMPLE OF LET
(defun first-last (llist)
(let ((f-list (first llist))
(l-list (last llist)))
(cons f-list l-list)) ; end let); end defun
(first-last ‘( a b c d )
returns ( a d )

13
DIFFERENCE BETWEEN
LET AND LET*
LET evaluates initial-value forms in parallel.
LET* evaluates initial-value forms
sequentially.
(setf x ‘AJAY)
(let ((x ‘VINAY)
(y x))
(list x y)
Result: (VINAY AJAY)

14
DIFFERENCE BETWEEN
LET AND LET* (Contd..)
(setf x ‘AJAY)
(let* ((x ‘VINAY)
(y x ))
(list x y)
Result: (VINAY VINAY)
; sequential evaluation updates values

15
PREDICATES & CONDITIONALS
• A predicate is a procedure that returns
a value that signals true or false; true is
signaled by T (anything other than NIL
is true for Lisp).
• False is signaled by NIL.

16
EQUALITY PREDICATES
EQUALITY predicates: - =, eq, eql, equal
= Tests whether two argument values are
same number or not.
(= 4 4.0) T (= 4 5) NIL (= 2 (+ 1 1))
T
(= 4 4) T (= 1 -1) NIL (= 3 (+ 1 –1))
NIL

17
EQ PREDICATES
EQ tests to see if its arguments
(represented by the same chunk of
computer memory) are same symbol or
not.
(eq 4 4.0) NIL
(eq ‘A ‘B) NIL
(eq ‘RAM ‘RAM) T
(eq 4 4) T
(eq 4.0 4.0) NIL 18
EQL PREDICATES
EQL first tests to see if its arguments
satisfy EQ, if not, it tries to see if they
are numbers of the same type and
values.

(eql 4 4.0) NIL

(eql 4 4) T
(eql 4.0 4.0) T
19
EQUAL PREDICATES
EQUAL tests if two argument values the
same expression.

(equal 4 4) T

(equal (+ 2 2) 4) T

(equal ‘( A B ) ‘(A B ))) T

(equal ‘( A B ) ‘(C D ))) NIL


20
MEMBER PREDICATES
WITH EQUAL
• Member tests only top level elements
with eql.
• To make member test with equal
(setf predicate #’ equal)
(member ‘(mother daughter) ((father son)
(mother daughter))
:test predicate) returns
(mother daughter)
21
DATA TYPE PREDICATES
• ATOM
• SYMBOLP
• NUMBERP
• LISTP
Test to check whether the argument is
an atom, symbol, number or list
respectively.

22
DATA TYPE PREDICATES
EXAMPLES
(Atom ‘pi) (Symbolp Nil) (LISTP ‘a b)
T T T
(Atom ‘5) (Symbolp 5) (LISTP ‘NIL)
T Nil T
(Atom ‘(a b)) (Numberp 5.2)(LISTP ( ))
Nil T T

23
NIL, NULL AND ENDP
• NIL and Empty list are equivalent as for as
LISP is concerned.
• NULL returns T if its argument is an empty
list otherwise returns NIL
(NULL ( )) (NULL ‘(THIS IS A LIST))
T NIL
• ENDP returns T if its argument (which must
be a list) is an empty list
(ENDP ‘SYMBOL) (ENDP ( ))
ERROR T
24
PREDICATES THAT WORK ON
NUMBERS
• The single argument is evaluated and is
supplied to the predicate.
numberp Is it a number?
zerop Is it zero?
plusp Is it positive?
minusp Is it negative?
evenp Is it even?
oddp Is it odd?
< Are they in decreasing order?
> Are they in increasing order?

25
EXAMPLES OF NUMBER
PREDICATES
(evenp (* 2 4 5)) T (plusp 5)T (minusp -2) T

(zerop 0) (plusp (+ 4 -5)) NIL

(> ‘( 2 4 5 6 )) T (< ‘( 1 0 5 8)) NIL

(> ‘( 5 4 8 9 )) NIL (< ‘( 5 4 3 2)) T

26
AND, OR & NOT Predicates
AND, OR & NOT are predicates that enable
elaborate testing.
AND
• Returns Nil if any of its arguments evaluates to NIL

• Evaluation takes place from left to right

• If any of AND’s arguments evaluates to NIL, the


remaining arguments are not evaluated.

• If all the arguments evaluate to something other than


NIL, the value of the last argument is returned.

27
EXAMPLE OF AND
(AND T T) (AND T T NIL) (AND (< 2 3) (< 3 4))
T Nil T
(setf animals ‘(Dog Cow OX Donkey Horse))
(and (member ‘cow animals)
(member ‘ox animals)
(ox donkey horse)
(and (member ‘lion animals)
(member ‘donkey animals)) NIL

28
OR PREDICATE

OR
• Returns NIL if all of its arguments
evaluate to NIL
• Evaluation takes place from left to right
• If any of OR’s arguments evaluates to
something other than NIL, none of the
remaining arguments is evaluated and
that non-NIL value is returned.
29
EXAMPLE OF OR PREDICATE
(setf birds ‘(crow parrot pigeon sparrow))
(OR (member ‘crow birds) (member ‘pigeon
birds))
(crow parrot pigeon sparrow)
(OR (T NIL NIL))
T
(OR (member ‘mina birds) (member ‘crow
birds))
(crow parrot pigeon sparrow)
30
NOT PREDICATE
NOT
Not turns non-NIL value into NIL and
NIL to T
(NOT NIL) T (NOT T) NIL
for LISP anything other than NIL is as
good as T
(NOT ‘DOG) is NIL

31
CONTROL FORMS :
IF, WHEN, UNLESS, COND

IF FORM
(if <test> <then form> <else form>)
Example : 1
(if oddp 5) ‘ODD ‘EVEN) ODD
(if oddp 8) ‘ODD ‘EVEN) EVEN

32
EXAMPLES USING IF
Example : 2
(defun abs-val (x)
; function to return the absolute value
(if (< x 0) – x x))
(abs-val -16) 16
(abs-val 5) 5

33
EXAMPLES USING IF
(Contd..)
Example : 3
(defun tellday (day)
(if (OR(equal day ‘Saturday)
(equal day ‘Sunday))
‘holiday ‘(working day)))
(tellday ‘Monday) (working day)
(tellday ‘Saturday) holiday

34
EXAMPLES USING IF
(Contd..)
Example : 4
(defun desig(code)
(if (equal code 1) ‘professor
(if (equal code 2) ‘reader
(if (equal code 3) ‘lecturer))) Nil)
(desig 1) (desig 2) (desig 3)
professor reader lecturer

35
WHEN FORM
WHEN can be used instead of an IF
whenever the false part of IF form
would be NIL.
(if <test> <then form> nil) is same as
(when <test> <then form>)
; when test is true then form is
evaluated.

36
EXAMPLE USING WHEN
FORM
Example: 1
(defun fever - or- not( )
(setf normal 98.4 temperature 102)
(when (> temperature normal)
(setf patient-temperature temperature)
‘(patient is having fever)) ; end when
(Return patien-temperature)); end defun

37
UNLESS FORM
UNLESS
Unless can be used instead of an IF
whenever the then form would be NIL.
(if <test> nil <else form>) ; when
test is false else form is evaluated.
(unless <test> <else form>)

38
EXAMPLE USING UNLESS
Example : 1
(defun cricket-runs(world-record
current-runs)
(unless (> world-record current-runs)
(setf world-record current-runs)
(list ‘new ‘record current-runs)))
(cricket-runs 300 320) →(new record 320)

39
WHEN AND UNLESS FORMS
Note:
WHEN and UNLESS can have an
unlimited number of arguments; the
first argument is the test form.

• Arguments between first and last are


evaluated for their side effects. The last
argument’s value is returned.

40
COND FORM
COND
COND chooses among alternatives
COND is followed by lists called clauses
(COND (<test 1> <consequent 1-1…>)
(<test 2> <consequent 2-1…>)
:
:
(<test m> <consequent m-1…>))
41
COND FORM (Contd..)
• Each clause contains a test, as well as zero
or more additional forms called consequents
• Only the test form in each clause is
evaluated.
• This is Repeated until the value of test form
is non-nil.
• The consequent forms of the triggered
clause are evaluated.
• The value of the COND form is the value of
the last consequent form in the triggered
clause.

42
EXAMPLE USING COND
FORM
The in-between consequent forms are there
only for their side effects
Example:
(defun computesarea ( )
; computes area depending on thing is
;circle or sphere
(setf thing ‘sphere r 1)
(cond ((eq thing ‘circle) (* pi r r))
((eq thing ‘sphere) (* 4 pi r r)))
12.56637 ; cond ); defun

43
EXAMPLE USING COND
FORM (Contd..)
If all the test forms are evaluated to NIL, the
value of COND form is NIL. To avoid this the
last test form is given as T

Example:
(defun testempty (L))
; tests whether L is empty or not
(cond ((NULL L) ‘EMPTY)
(T ‘NOT-EMPTY))
(testempty ( )) EMPTY
(testempty ‘(a b)) NOT-EMPTY
44
COND FORM (Contd..)
NOTE: If only a test form is there in a
clause, the value of the test form is the
value of the COND form. COND is
useful when there are more than two
cases to consider.

45

You might also like