CSci 1901: Lab 9, March 22

Lab 9: Local State and Hilbert Space Filling Curves

Related files:

  1. Step 1: A simple procedure with local state 2 points

    Below is a small procedure called make-withdraw from the book.

    (define (make-withdraw balance)
      (lambda (amount)
        (if (>= balance amount)
            (begin (set! balance (- balance amount))
                   balance)
            "Insufficient funds")))
    

    Modify the above code so that it does not take in a balance parameter. In doing this assume every account begins with an initial balance of 300 dollars. The code should then work as follows:

    STk> (define a1 (make-withdraw))
    STk> (a1 50)
    250
    STk> (define a2 (make-withdraw))
    STk> (a2 40)
    260
    STk> (a1 40)
    210
    STk> (a1 250)
    "Insufficient funds"   
    
  2. Step 2: A line drawing procedure with local state - 4 points

    The gdraw file you are given contains a procedure, (draw-line x1 y1 x2 y2), that draws a line on the gdraw canvas from coordinates (x1, y1) to (x2, y2). Using this procedure the following sequence of calls would draw a rectangle on the screen.

    STk> (draw-line 0 0 20 0)
    STk> (draw-line 20 0 20 30)
    STk> (draw-line 20 30 0 30)
    STk> (draw-line 0 30 0 0) 
    

    In many applications it nice to have an easy way to draw lines like the above example where each line is assumed to start where the last line ended. This allows you to easily specify continous line structures one coordinate at a time. If you had a procedure that did this, say line-drawer, the above rectangle could be drawn with the sequence of calls:

     
    STk> (line-drawer 0 0)   ;; First call specifies where to start
    STk> (line-drawer 20 0)  ;; Draws line from (0,0) to (20,0)
    STk> (line-drawer 20 30) ;; Draws line from (20,0) to (20,30)
    STk> (line-drawer 0 30)  ;; etc
    STk> (line-drawer 0 0)    
    

    line-drawer can be implemented as procedure with local state. Write a procedure (make-line-drawer) that returns a procedure with local state the operate like line-drawer in the above example. If your code works, the following calls should draw a square and a rectangle to the screen.

    STk> (define line-drawer (make-line-drawer))
    STk> (line-drawer 0 0)   
    STk> (line-drawer 20 0)  
    STk> (line-drawer 20 30) 
    STk> (line-drawer 0 30)  
    STk> (line-drawer 0 0)
    
  3. Step 3: Drawing Hilbert Curves - 4 points

    Read through the information on Hilbert curves found here, and use the pseudo code found halfway down the page to implement a scheme procedure, called hilbert, that draws Hilbert space filling curves. Your hilbert procedure should take in a line drawing procedure created with make-line-drawer to implement the LineTo pseudo code call described in the link above. An outline of your procedure should look like:

    (define (hilbert x0 y0 xis xjs yis yjs n line-drawer)
      ;; Use line-drawer for the LineTo commmand
      ...
    )
    

    After running the test cases in the lab template, four Hilbert curves should be drawn on the gdraw canvas that look like the image at the top of this file, or some rotation of it.

Bonus Questions

Congratulations on completing Lab 9!
Lab 9 total: 10 pts

This is what you will learn in this lab

Copyright: © 2006 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini
Changes and corrections are in red.