### 2017-09-27

posted Sep 27, 2017, 6:10 AM by Samuel Konstantinovich
 Do Now: (On Paper)Remember the quadratic formula:1. Write a function to calculate the discriminant (the part INSIDE the square root)(define (disc a b c)  ???)2. Write out two EASY quadratics that you can solve. Make sure that at least one of them has roots that are not equal to each other. Solve these (you can use factoring or the formula)Classwork:3. Write a function (quadsolve a b c sign) since there are two results from this formula, and a function in scheme returns a single value, we need to handle the +/- operator as two separate formulas. -Your function should return EITHER the + version or the - version of the formula.-Chooses it based on a fourth parameter sign which can be any negative or positive value (non zero):Example of usage:(quadsolve 1 0 -9 2)   ; is 3(quadsolve 1 0 -9 -1) ; is -3Goal : Recursion... A function can make a call to itself:;YOU HAVE TO BE CAREFUL:(define (bar x)   (+1 (bar x)))  ; THIS IS BAD!;infinite recursion occurs when you ;repeatedly make calls to the same function and never stop.;this is better idea, use an if statement (or a cond);if it is negative, try again with the positive:(define (foo n)  (if (< n 0)      (foo (- n)) ;When n is negative, try again on positive.       (+ n 1)))   ;When this triggers, it stops!(foo -3) ; is 4(foo 3) ; is also 4;Sometimes we use recursion to repeat things, or use;many numbers:(define (sumall n)  (if (<= n 1)      1 ;this is when to stop      (+ n (sumall (- n 1)))))  ;n must get closer to stopping      ;this gets closer to 1, so it will always terminate.(sumall 5)
Comments