COMP 210, Spring 2001
Homework 11 : palettes, zoos, passwords
Due Nov.21 (wed), at start of class

I encourage you to start these problems on your own, and then meet with your partner to discuss differing facets (large or small), and taking the best features of each, to turn in. This approach works (and is allowed) only if both of you work on the problem in advance, bringing it to a similar state of completion.

You are NOT required to show the template for your programs (woo-hoo!). Your functions should still follow the template, though. And of course, each function should still have a contract and purpose, and reasonable test cases.

Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks (in particular: staple and provide questions), and the Grading Guidelines.


  1. (4pts) Colors can be decomposed into a red, green, and blue component; computer systems often represent colors as a triple of numbers: each component as an integer between 0 (black) and 255 (saturated), inclusive. (Note: these are additive colors.) Computer applications often let the user select a color by having them specify (say) the blue component with a slider, and then displaying a (say) palette of 10x10 color choices (each choice being a reasonably sized square).

    Write a function which takes a value for the blue component, and draws a corresponding palette. (each color a reasonable-sized square, and colors which pretty much cover the desired spectrum).

    Of course, be sure to not have arbitrary (unnamed) constants occurring in your code. You can use any suitable variation on lecture's fori=.

    This problem uses the teachpack color.ss (located in /home/comp210/Homeworks/Hw11/). This teachpack includes all the functions of the draw teachpack (like start and draw-solid-rect, plus one more function:

    ;; make-color: num, num, num --> color
    ;; Takes in the red, green, and blue components of a color
    ;; (each in [0,256) ) and returns a color object suitable
    ;; for use with draw-solid-rect.
    ;;
    (define (make-color r g b) ...)
    
    To use the teachpack at home, you need to copy that file. color.ss.

  2. (3pts)

    (define-struct animal (name type))
    
    (define ZOO-SIZE 3)
    
    (define bobs-petting-zoo
       (make-vector  ZOO-SIZE             (make-animal 'dolly 'sheep)))
    (define san-diego-zoo
       (build-vector ZOO-SIZE (lambda (i) (make-animal 'dolly 'sheep))))
    
    1. How many times is make-animal called, altogether?
    2. For each of bobs-petting-zoo ("bpz") and san-diego-zoo ("sdz"), write (and evaluate) an expression which transmutes the animal at index 1 into a llama. ("They called me mad, at the institute!")
    3. Draw the resulting picture of what things look like after evaluating the previous two parts (as per pictures in lecture). (Your answer should reveal to you why bobs-petting-zoo and san-diego-zoo don't look similar.)

  3. (5pts) In this problem we will create an entity which has its own password, and which checks for possible security breakins.

    Write a function make-passwd-object that takes an initial password and returns a "password object". This password object is a function that takes two inputs, an action (a symbol) and a list.

    There are two possible actions.

    1. The first possible action is 'check-passwd. In this case the list argument contains only one item, the password to be checked. If this password agrees with the current password for the object, then the result of the function call is true. If the password is not correct, the object returns false.
    2. The second possible action is 'set-passwd. In this case the list argument contains two items, the old password and a new password. If the old password agrees with the current password for the object, then the current password is updated to be the new password. Otherwise, the current password for the object remains unchanged. The return value is a boolean indicating whether or not the password was successfully changed.

    To prohibit repeated attempts to guess the current password, the object should keep track of failed attempts to check a password or update a password. If there are 4 or more consecutive failed attempts, the object should lock the password object, causing the action invoked via 'check-passwd or 'set-passwd to always fail. I should not be able to write a function which takes in the password object and circumvents this, even if I can look at your code.

    Hint: Maintain the current password and number of consecutive failed attempts as local variables. (Lectures friday and monday will explore some interaction between set!, local, and lambda, though you already have all the knowledge you need for this assignment.)

    Example (which you can use as your entire test suite):

    (define my-passwd (make-passwd-object 'secret-handshake))
    (define your-passwd (make-passwd-object 'open-sesame))
     
    (my-passwd 'check-passwd (list 'is-this-it?))                    ;  = false
    (my-passwd 'check-passwd (list 'secret-handshake))               ;  = true
    (my-passwd 'set-passwd   (list 'I-forgot 'treasure-map))         ;  = false
    (my-passwd 'check-passwd (list 'treasure-map))                   ;  = false
    (my-passwd 'check-passwd (list 'secret-handshake))               ;  = true
    (my-passwd 'set-passwd   (list 'secret-handshake 'pet-name))     ;  = true
    (my-passwd 'check-passwd (list 'pet-name))                       ;  = true
    (my-passwd 'check-passwd (list 'clumsy-breakin-attempt))         ;  = false
    (my-passwd 'check-passwd (list 'clumsy-breakin-attempt))         ;  = false
    (my-passwd 'check-passwd (list 'pet-name))                       ;  = true
    (my-passwd 'check-passwd (list 'another-clumsy-breakin-attempt)) ;  = false
    (my-passwd 'check-passwd (list 'if-at-first)                     ;  = false
    (my-passwd 'check-passwd (list 'i-dont-succeed))                 ;  = false
    (my-passwd 'check-passwd (list 'persistence-gets-me-arrested))   ;  = false
    (my-passwd 'check-passwd (list 'pet-name))                       ;  = false
    (my-passwd 'set-passwd   (list 'pet-name 'trojan-horse))         ;  = false
    (your-passwd 'check-passwd (list 'open-sesame)                   ;  = true