Presentation is loading. Please wait.

Presentation is loading. Please wait.

Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.

Similar presentations


Presentation on theme: "Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond."— Presentation transcript:

1 Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1) )) Recursion: Doing loops in Lisp (Factorial N) is equal to the product of all numbers from N down to 1. (Factorial 5)=5*4*3*2*1 = 120 If the number is > 1 Do the multiplication Take one away from the number Repeat the operation with the smaller number There is no for; to repeat an operation we write a function that calls itself: a recursive function Notice that all the same operations get done; just in a different order.

2 Recursion in lisp A recursive function is a function which calls itself as a subroutine; as a helper to solve a problem A recursive function works as follows. When it is asked a question (given an argument), it 1)Checks if the argument is so simple it can give the answer straight away. If so it gives the answer and stops. 2) If the argument is too “big” to give an immediate answer it Computes a smaller version of the argument Calls itself as a helper to get an answer for the smaller arg Combines that answer with the left-behind part of the “big” argument, to get the overall answer to the original question

3 1) Check if the argument is so simple it can give an answer straight away. If so, give the answer & stop Calls itself as a helper to get an answer for that smaller argument (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) Compute a smaller version of the argument Combines that answer with the left-behind part of the “big” argument, to get the overall answer to the original question Parts of the recursive function 2) If the argument is too big to give an immediate answer

4 1. Make a copy of the function and replace N by 2 (defun factorial(2) (cond ( (> 2 1) (* 2 (factorial (- 2 1)))) ( (= 2 1) 1) ) ) 2. Evaluate the body of the function (cond ( (> 2 1) (* 2 (factorial (- 2 1)))) ( (= 2 1) 1) ) 3a. Evaluate (- 3 1) (* 3 (factorial (- 3 1) ) ) 3b. Evaluate (factorial 2) (* 3 (factorial 2) ) 3c. Multiply by 3 (* 3 (factorial 2) ) 1. Make a copy of the function and replace N by 3 (defun factorial(3) (cond ( (> 3 1) (* 3 (factorial (- 3 1)))) ( (= 3 1) 1) ) ) 3a. Subtract 1 from 2 (* 2 (factorial (- 2 1))) 3b. Evaluate (factorial 1) (* 2 (factorial 1) ) 3c. Multiply by 2 (* 2 (factorial 1) ) 3. (> 2 1) is true (t); evaluate (* 2 (factorial (- 2 1))) 2. Evaluate the body of the function (cond ( (> 3 1) (* 3 (factorial (- 3 1)))) ( (= 3 1) 1) ) ) 3. (> 3 1) is true (t); evaluate (* 3 (factorial (- 3 1))) (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) How does recursion work? > (factorial 3) 6 2 1. Make a copy of the function and replace N by 1 (defun factorial(1) (cond ( (> 1 1) (* 2 (factorial (- 1 1)))) ( (= 1 1) 1) ) ) 2. Evaluate the body of the function (cond ( (> 1 1) (* 1 (factorial (- 1 1)))) ( (= 1 1) 1) ) ) 3. (= 1 1) is true (t); just return 1 as the answer ( t 1) 11 2 6 2 2 1 6 Tracing a call to the function:

5 Every recursive function has A stopping condition; when this is true the answer can be returned directly with no more function calls In the recursive condition the function always calls itself with “smaller” or “simpler” arguments (defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) ) ) A recursive condition: when this is true the function must call itself to get an answer (going around the loop) Without both a stopping condition and smaller arguments, you get infinite recursion; a neverending loop. The answer is built up by taking the answer from the recursive call and doing something to it

6 Recursion with lists Lists are central in lisp To process a list (to find an element, or do something to all elements) we need to do some operation over and over: a loop We write a recursive function with lists as arguments. A simple task: count the number of elements in a list Suppose L is the list we are looking at. Our recursive function will use (first L) to select the current first element of the list and do something to it It will use (rest L) to get a smaller part of the list, and do the recursive call (the loop) with that smaller list.

7 count the number of elements in a list (+ 1 ) ( t ) (len ) This is going to be a loop where we consider each element of a list (count that element) when we’ve counted an element, go on to rest of the list stop when we get to the end of the list Stopping condition(null L)(cond ( ) ) Function definition (defun len(L) ) recursive condition (rest L) 0

8 Comparing two recursive functions (Defun len(L) (cond ( (null L) 0) ( t (+ 1 (len (rest L)))) )) (Defun factorial(N) (cond ( (> N 1) (* N (factorial (- N 1)))) ( (= N 1) 1 ) )) Stopping conditions? Recursive conditions? The t in len means “otherwise”. We could rewrite factorial to use t. (Defun factorial(N) (cond ( (= N 1) 1) ( t (* N (factorial (- N 1)))) ))


Download ppt "Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond."

Similar presentations


Ads by Google