Comp 200 Lecture
Exercises: Fractals, number-of-digits

In class we began talked about a Koch curve, (which looks like a Mt Kiliminjaro). More precisely, that was a level one Koch curve. A level two Koch curve is like level one, except that each straight-line segment is replaced with a copy of the original level-one Koch curve (of appropriate size).

To draw a level-three Koch curve, draw a small level-two curve, turn, draw another small level-two curve, turn again, draw a third level-two curve, turn, and finally draw the last level-two curve.

And it goes on: To draw a level-17 curve, draw a level-16 curve, turn, draw another level-16 curve,

A level three Koch curve is like level two, except that each of the level two's segments is replaced with a smaller copy of the (level-one) Koch curve. An alternate way of looking at it is that a level-n Koch curve is like a level-one curve, except that each segement is replaced with a smaller level-(n-1).

Fill in the blanks below, and try it out!

;; koch: natNum,number,before --> turtle-snapshot 
;; (koch level length) draws a koch curve level levels 
;; deep, a distance of length, based on the snapshot before.
;; It returns a new snapshot.
;; 

(define (koch level length before)
    (if (zero? level)
        ...                                                     ; What is a zero-level koch curve?
        ... ... ... (koch (sub1 level) ... before))... ... ...) ; What does (koch (sub1 level) ... before) give us?
Hint: How do you make a koch curve level-1 deep and only a third of length long? How do you make four smaller ones with turns between them? (When finished, we can compare solutions new!.)

Now, can you make your code easier for the user to call? That is, a function (snowflake level size), so the user doesn't have to provide an before snapshot?

If you like, you can play with this. Draw three koch curves in a row, but with a -120 turn between them. (Hence the name "snowflake".)

The Koch curve is just one particular fractal curve. Try making up your own base designs, and see what you get! It is not always as easy as it might seem! (Hint: make sure the turtle starts and ends pointing in the same direction. Or, take your Koch curve and see what you get if the turtle doesn't end up quite facing the same way as started.) You can try looking at some other examples.


Exercise: Write a function bullseye which draws several "levels" of concentric squares. That is, make a square of size 10, then one of size 9, then size 8, etc.
Hint: Write a helper function which draws a single square. Where should the turtle be before drawing the square? After?
There are a couple of ways you can modify your function: Either

The next few exercises aren't graphical, but segue into the next major topic of the course.

Exercise: Write the function timesHalved, which takes a number n and returns how many times i could halve that number before it gets smaller than one. (Think, how many times you could bet half your wealth and (sadly) lose, until you had less than a dollar?)

Exercise: Write the function insert, which takes a number n and a list-of-numbers lon which is sorted (in ascending order). It returns a new list of numbers which contains n inserted into lon in the correct place.

;; insert: num,list-of-num --> list-of-num
;; (insert n lon
;; lon is assumed to be sorted in ascending order.
;; Return a new list of numbers which contains n inserted
;; into lon in the correct place.
;;
(define (insert n lon)
     (if (null? lon)
         ...n...   ;; How to insert something into the empty list?
         ...n...(first lon)...(insert n (rest lon))...))
                   ;; How to combine these three things to get the desired list?
Here are some test cases for you:
(insert 15 null)
(insert  4 (list 7 9 11))
(insert 15 (list 10))
(insert 15 (list 7 9 11))
(insert 10 (list 7 9 11))

Exercise: We will write the function sort, which takes a list of numbers, and sorts it. How on earth to write this? Well, let's start with the generic recipe we've seen repeatedly for lists, and see if that helps.

A list of numbers is either

  1. null, or
  2. (cons n lon), where n is a number, and lon is a list of numbers.
We've seen that functions which handle lists-of-numbers tend to have a shape similar to the shape of the above definition:
(define (sort l)
     (if (null? l)
         ; How to sort the empty list?:
         ...             
         ; What are these two things?  How to combine them to get l, sorted?
         ...(first l)...(sort (rest l))...))
Talking it over with your partner and labbie, fill in the dots! Make up your own test cases, and try them!