03-mutation-post3up

03-mutation-post3up - Mutation Readings: HtDP, section 39,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Mutation Readings: HtDP, section 39, and optionally sections 40, 41, 42. We will use a somewhat different approach to the ideas in these sections, and use some Racket features not discussed in the textbook. “As Yoda might say: Assignment leads to mutation. Mutation leads to pointers. Pointers lead to suffering!” (Anton van Straaten, PLT Scheme mailing list, 2001–12–24) We will also take our first look at a C program. CS 136 Spring 2011 03: Mutation 1 Our first look at C In CS 135, we saw that a direct translation of the recursive definition of Fibonacci numbers ( f = 0 , f 1 = 1 , f n = f n- 1 + f n- 2 ) resulted in a very inefficient function. ( define ( fib1 n ) ( cond [( zero? n ) ] [( = n 1 ) 1 ] [ else ( + ( fib1 (- n 1 )) ( fib1 (- n 2 )))])) The running time of this function is O ( c n ) , where c ≈ 1 . 618 . This is an example of exponential running time. CS 136 Spring 2011 03: Mutation 2 Here is the same function written in C: int fib1( int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fib1(n-1) + fib1(n-2); } } What are the differences? CS 136 Spring 2011 03: Mutation 3 The Fibonacci function Both implementations are recursive in nature and have the same running time (but as we will see later in the course, it is not always the best idea to use recursion in C) We can use accumulative recursion to achieve a better running time CS 136 Spring 2011 03: Mutation 4 An accumulative solution ( define ( fib2 n ) ( local (( define ( fib-acc t ftm1 ft ) ( cond [( = t n ) ft ] [ else ( fib-acc ( add1 t ) ft ( + ft ftm1 ))]))) ( fib-acc 1 0 1 ))) Its running time is O ( n ) . CS 136 Spring 2011 03: Mutation 5 This corresponds to the following C code: int fib_acc( int n, int t, int ftm1, int ft) { if (t == n) { return ft; } else { return fib_acc(n, t+1, ft, ft+ftm1); } } int fib2( int n) { return fib_acc(n, 1, 0, 1); } CS 136 Spring 2011 03: Mutation 6 Completing the C Program It is necessary to provide a main function. This function is automatically called when a C program is executed. Here is a sample main function: #include <stdio.h> // implementations of the functions fib1, fib2, // and fib_acc appear here int main( void ) { printf("fib2(5) = %d\n", fib2(5)); return 0; } CS 136 Spring 2011 03: Mutation 7 #include <stdio.h> is an instruction to the C preprocessor, which re-writes the program before the compiler processes it....
View Full Document

This note was uploaded on 06/19/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

Page1 / 10

03-mutation-post3up - Mutation Readings: HtDP, section 39,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online