Courses‎ > ‎Introduction to Computer Science 1‎ > ‎Konstantinovich‎ > ‎ML1 9/2012‎ > ‎labs‎ > ‎

Lab 04

posted Oct 14, 2012, 8:44 PM by Samuel Konstantinovich   [ updated Dec 21, 2012, 5:24 AM ]
;The goal today is to learn about the begin and display commands. 
;Used together they can show us how a recursive function progresses!

;The begin statement executes each command one after another
;the whole statement evaluates to the last part
;so in the case of countTo2, the last part is a display
;which has no numerical value, 
;CountTo1 has a countTo1 call last, which ultimately becomes 0. 
;notice that depending when you display the result, the numbers 
;appear in opposite order!

(define (countTo1 x)
  (if (> x 0)
        (display x)
        (countTo1 (- x 1)))
        (display x)
(define (countTo2 x)
  (if (> x 0)
        (countTo2 (- x 1))
        (display x))
        (display x)

(display "countTo1(4)=")
(countTo1 4)
(display "countTo2(4)=")
(countTo2 4)
;Why is this useful to you?
;Haven't you wanted a way to see what values it finds as 
;it goes through the recursion? Haven't you wanted a way to 
;make it print the values along the way to the end result?
;Now you can! We don't normally print the values in scheme, but
;we can (display value) if we wanted to. 

;Part 1: Sample sequence:
(define (sequence n)
   (= n 0)
   0;terminal value
     ;instead of adding 1/n to the sum
     ;I just display it. 
     (display (/ 1 n))
     (display ",")
     (sequence (- n 1)))))

(display "\nSequence(8)=")
(sequence 8)

;Hailstone series
;Hailstone of n is defined as:
;n/2 for even values of n.
;3n+1 for odd values of n.
;Stop when n=1. 
;Since there are 3 possibilities... that should SCREAM: cond

;lets say n=5
;3(5)+ 1 -> 16
;(16)/2 -> 8
;(8)/2 -> 4
;(4)/2 -> 2
;(2)/2 -> 1
;OR:  5,16,8,4,2,1
;Lets say n=3
;simple enough... if not google hailstone sequence and read more!

;So the final number in the sequence is always one. 
;This means that knowing the final value is not very interesting, 
;seeing the entire sequence is FAR more interesting. 

;Your goal is to write a function that DISPLAYs the sequence
;(hailstone 32) would show:
;This is similar to the sample sequence except you don't change n by
;subtracting 1 every time.

;Once you get the working sequence, write a function that tells you
;how long the hailstone of a number is.
;(hailLength 1) should be 1
;(hailLength 32) should be 6
;(hailLength 3) should be 8  ... see the sequences above

;Write a function that tells you the maximum hailstone value of the sequence. 
;(maxHail 32) -> 32
;(maxHail 3) -> 16
;(maxHail 5) -> 16