Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.

Similar presentations


Presentation on theme: "Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures."— Presentation transcript:

1 Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures –list length –member of a list

2 Basics of recursion The basic elements of a recursive function are: a base case: –the condition for using the base case –the solution in the base case a recursive case –the condition for using the recursive case –the solution in the recursive case decomposes the problem into smaller subproblems recursively solves subproblems combines subproblem solutions to solve overall problem

3 Example: factorial ;;; Scheme implementation (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))) ))) fact(0) = 1 fact(n) = n * fact(n-1)

4 (* ML implementation *) fun fact(0) = 1 | fact(n) = n * fact(n-1); Example: factorial fact(0) = 1 fact(n) = n * fact(n-1)

5 ;;; Scheme implementation (define fib (lambda (n) (if (<= n 1) 1 (+ (fib (- n 1))(fib (- n 2))) ))) Example: fibonacci fib(0) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2)

6 (* ML implementation *) fun fib(0) = 1 | fib(1) = 1 | fib(n) = fib(n-1) + fib(n-2); Example: fibonacci fib(0) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2)

7 ;;; Scheme implementation (define len (lambda (lst) (if (null? lst) 0 (+ 1 (len (cdr lst))) ))) Example: list length len( empty list ) = 0 len( non-empty list L) = 1 + len(tail of L)

8 (* ML implementation *) fun len([])= 0 | len(hd::tl) = 1 + len(tl); Example: list length len( empty list ) = 0 len( non-empty list L) = 1 + len(tail of L)

9 Example: member ;;; Scheme implementation (define member (lambda (item lst) (if (null? lst) #f (or ;;; short circuit evaluation (eq? item (car lst)) (member item (cdr lst)) )))) member(item, empty list ) = false member(item, non-empty list L) = true if item is (first L), member(item,tail of L) otherwise

10 (* ML implementation *) fun member(item,[])= false | member(item, hd::tl) = (item=hd) orelse member(item,tl); Example: member member(item, empty list ) = false member(item, non-empty list L) = true if item is (first L), member(item,tail of L) otherwise

11 Tail recursion Tail recursion is a special case of recursion in which the recursive call is the last thing done in a function. This sort of recursion can be optimized to reuse the environment which binds the parameters of the recursive function to their arguments. This is a special case of what is known as last-call optimization.

12 Writing tail recursive functions To make a recursive function tail recursive we transform a recursion with work remaining to do after the recursive call into a recursion where that work is done prior to the call. Let’s use factorial as an example.

13 Example: factorial ;;; recursive Scheme implementation (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))) ))) ;;; not tail recursive: after recursive call, must multiply by n fact(0) = 1 fact(n) = n * fact(n-1)

14 Example: factorial ;;; tail recursive implementation (define tr_fact (lambda (n acc) (if (= n 0) acc (fact (- n 1) (* n acc)) ))) ;;; tail recursive: (* n acc) done before recursive call! fact(0) = 1 fact(n) = n * fact(n-1)

15 Hiding helper ;;; tail recursive implementation (define fact (letrec ((helper (lambda (n acc) (if (= n 0) acc (fact (- n 1) (* n acc)) ))) (helper n 1) ))


Download ppt "Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures."

Similar presentations


Ads by Google