Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.

Similar presentations


Presentation on theme: "Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing."— Presentation transcript:

1 Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing with those arguments –returns a single answer (evaluates to that answer) Everything within the function is invisible to the user; all they see is the single answer returned.

2 Iterative (imperative) programming –tell the computer to execute a series of actions in a given order: int A= 3 * 3; int B= 4 * 4; int C= A + B; int Ans= sqrt (C); println(Ans); 5 Functional programming –Ask the computer to evaluate an expression made up of function calls: >(sqrt (add (square 3) (square 4) )) 5 We want to write a function to do this for any numbers, not just 3 & 4. Getting the hypotenuse of a right-angle triangle

3 Defining a function in a list (defun hypotenuse (x y) (sqrt (+ (square x) (square y))) ) 1 st element: Special term defun (define function) 2 nd element: name of the function Last element: What function computes 3 rd element: list of function arguments In lisp, the code defining a function is given in a list Given a set of arguments, every function returns (evaluates to) the value of the last element in this defining list

4 We can tell the interpreter (the listener) to define our function for us, and then we can use it to compute things > (defun hypotenuse (x y) (sqrt (+ (square x) (square y))) ) HYPOTENUSE > (hypotenuse 3 4) 5 What happens when we call (hypotenuse 3 4) like this? > (hypotenuse 3 4) 1.The lisp interpreter looks up “hypotenuse” in the list of functions it knows 2.The interpreter gets the arguments in the function call, and matches them with the argument names in the function definition 4.The interpreter works out the value of this expression (just as if it was typed in directly) and returns the answer 5 (sqrt (+ (square 3) (square 4))) 5 (sqrt (+ (square 3) (square 4))) 3.The interpreter makes a copy of the body of the function, and replaces argument names with arguments values in that copy

5 The arguments to a function can be: “Raw” data > ( sqrt ( + (square 3) (square 4) ) ) > ( sqrt ( + 9 (square 4) ) ) > ( sqrt ( + 9 16 ) ) > ( sqrt 25 ) 5 >(+ 16 9) A function call This is because a function call always evaluates to (turns into) raw data. Everywhere you could use raw data, you can use a function call that evaluates to that data. 25 >(sqrt 25) 5

6 In lisp, t means true, nil means false. Logical predicates in lisp >(= 2 2) t >(= 5 3) nil Note that “=“ is a function just like any other. It comes first in the list, it is followed by two arguments, and it evaluates to either t or nil. “=“ is a function for comparing two numbers. We can use a function call wherever we use raw data >(= (hypotenuse 3 4) 5) t

7 Conditional operators in lisp: if In lisp, an if statement is a list with four elements (defun abs(X) (if (> X 0) X (* -1 X) ) ) 1 st element: Special term if 2 nd element: logical test function Last element: value returned if test is false 3 rd element: value returned if test is true if X > 0 then return X, else return (X by –1) If is a function evaluating either to its then component (3 rd element) or its else component (4 th element)

8 Cond: a general conditional operator If only allows for a single logical predicate test Cond is more general, allowing a whole sequence of logical predicate tests Lisp tests the predicates in order until one evaluates to t. It then returns the consequent value. (defun stupid(X) (cond ( (= X 1) ‘one ) ( (= X 2) ‘two ) ( (= X 3) ‘three ) ( t ‘huh? ) ) ) Special term cond A clause, consisting of a test and a consequent Note that this test is always true; this is like the “else” part of if

9 VERY important logical predicates =Two numbers are the same eq Two atoms are the same (eq ‘cat ‘cat) -> t equalTwo lists are the same (equal ‘(cat dog) ‘(cat dog)) -> t nulla list is empty (null ‘()) -> t listpsomething is a list (listp ‘(cat dog)) -> t atompsomething is an atom (atomp ‘cat) -> t


Download ppt "Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing."

Similar presentations


Ads by Google