2018-03-19 Recursion + HW

posted Mar 19, 2018, 6:17 AM by Konstantinovich Samuel   [ updated Mar 19, 2018, 7:03 AM ]

Homework 
1. Complete the last trace, on paper... and re-familiarize yourself with scheme.
2. Submit your alice assignment on the homework server. 
use "alice-simple.txt" as your file name!!!!!

You should be able to:
-read in a file
-split it appropriately
-get all of the numbers/words
-Process the data somehow


Do Now:
Discuss this with your neighbor and write out each way on paper:
1. What commands would you use to get EACH of the numerical values from a text file in the formats below and calculate a sum. 

( use the name "filename.txt"
Assume there is an unknown number of values in the same format.
a) "12.3
4.4
592.1"
etc.  (write all the code and Number your lines to make b and c easier )
b) "12.3 4.4 592.1" etc...  (feel free to just write the differences)
c) "12.3,4.4,592.1" etc...  (feel free to just write the differences)


Goal: What is recursion? 


When you google recursion... it asks you "did you mean recursion?"




Good tracing practices:
When you need to work out a separate function call draw an arrow to another row/column and do it there. When you figure out the answer, draw an arrow back with the result. This is a good way to trace your code!


Consider the following functions in various languages:

 def what(s):
   if s <= 0:
     return 0
   else:
     return s + 1 + what(s-2)

2. ON PAPER - Evaluate:
what(4)
what(8)

to-report fix [ a ] ifelse a < 10 [ report a + fix (a + 1)] [ report 1] end

3. ON PAPER - Evaluate:
fix 8
fix 5



(define (foo x)
 (if (< x 1)
   1
   (* x (foo (- x 1)))))

(define (bar x y)
  (cond
    (( > x y) 0)#base case
    (( = x y) x)#base case
    (else (+ x y (bar (+ x 1) (- y 1))))))

4. ON PAPER - Evaluate:
(foo 6)        
(bar 1 3)        
(bar 1  10)




Comments