Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.

Similar presentations


Presentation on theme: "Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively."— Presentation transcript:

1 Scheme examples

2 Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.

3 Basic Strategy Define the base case(s) as the termination points Use CAR and CDR to extract components from the list and then (if necessary) make recursive calls on the remainder of the list. The “remainder of the list” should be getting smaller on each call to assure termination.

4 Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) int Addup (a,n) { sum=0; for (i=0, i<n; i++) sum+= a[i]; return sum; } in lisp Summing elements in a list(table) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work! int Addup (a,n) { sum=0; if (n==1) return a[0]; else return a[0]+Addup(a[1],n-1) } Recursively in c

5 Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) Summing elements in a list(table) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work!

6 Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) Summing elements in a list(table) ( Addup (3 6 2) ) initial call a NULL? recursive call (3 6 2)false ( + 3 (Addup (6 2) ) ) next call(6 2)false ( + 6 (Addup (2) ) ) next call(2)false ( + 2 (Addup ( ) ) ) next call( )true -> return 0 No recursive call

7 Iteration NOW RETURN RESULTS BACK ( Addup (3 6 2) ) initial call a NULL? recursive call (3 6 2)false ( + 3 (Addup (6 2) ) ) next call(6 2)false ( + 6 (Addup (2) ) ) next call(2)false ( + 2 (Addup ( ) ) ) next call( )true -> return 0 No recursive call ( + 2 0 ) 0 2 ( + 6 2 ) 8 ( + 3 8 ) 11

8 Simple member function (DEFINE (member atm lis) ( COND ( (NULL? lis) '() ) ( (EQ? atm (CAR lis)) #T) (ELSE (member atm (CDR lis) ) ) ) ) Recursive call (iteration) Tests (nested if) If test is true

9 Equality of simple lists (DEFINE (equalsimp lis1 lis2) (COND ( (NULL? lis1) (NULL? lis2) ) ( (NULL? lis2) '() ) ( (EQ? (CAR lis1) (CAR lis2) ) (equalsimp (CDR lis1) (CDR lis2) ) ) (ELSE '() ) )) What happens with a list like ( a ( b c ) d ) ?

10 Equality of arbitrary list (DEFINE (equal lis1 lis2) (COND ( (NOT (LIST? lis1)) (EQ? lis1 lis2)) ( (NOT (LIST? lis2)) '() ) ( (NULL? lis1) (NULL? lis2)) ( (NULL? lis2) '()) ( (equal (CAR lis1) (CAR lis2) ) (equal (CDR lis1) (CDR lis2))) (ELSE '() ) )) Files can be found on the web site under PRACTICE link


Download ppt "Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively."

Similar presentations


Ads by Google