8.15

Homework 6🔗

home work!

Programming Language BSL with List Abbreviations

Due Date: Wednesday February 19, 9pm

Purpose To practice using lists; explore abstractions

Expectations

-----------------------------------------------------------------------------

In this and future homeworks, when using lists, you can just start using a type like [List-of Foo] without having to define that list data type first (but you will need to have defined the data type Foo, unless it is built-in).

Exercise 1 Abstracting Functions: Assume you have a data type SmallNatural defined that represents integers in the range 0-1000. You do not have to call out to a helper function to work with this type. This exercise is a slightly trickier abstraction process than functions like prefix-with-from/prefix-with-to that we did in class, so think about it a bit.
  • Design the function keep-greater-than that consumes a list of SmallNatural and a number n, and produces a list of those values in the list that are equal to or greater than n.

  • Design the function keep-less-than that consumes a list of SmallNatural and a number n, and produces a list of those values in the list that are less than or equal to n.

  • Design the function keep-select that abstracts the functionality of keep-greater-than and keep-less-than.

  • Re-write the implementation of keep-greater-than and keep-less-than to use the abstract function keep-select. Name the new functions keep-greater-than/v2 and keep-less-than/v2, so that the system won’t complain about your redefining a function name.

    For this part, you are only redoing the last step of the function design recipe: the code. The first three parts of the recipe would be the same as for your original versions. However, since these functions have new names, you should copy/paste and adapt the original check-expects to test that your new functions work correctly.

Exercise 2 Towards the Project In this exercise, the data types and functions you design will form the first pass of a project that will span three (non-sequential) homeworks. Work on them carefully.

  • Design the function count-trues that consumes a list of Booleans, and returns the number of #true values in the list.

  • Design the function nth-is-true? that consumes a list of Booleans and a number n, and returns whether the value at position n (counting from 0) is #true.

  • Design the function first-true that consumes a list of Booleans, and returns the position (counting from 0) of the first #true, or -1 if none were found. Avoid unnecessary recursion.

  • Design the function set-true that consumes a list of Booleans and an index n, and returns the list with the n-th item in the list (counting from 0) converted from #false to #true; if the old value was not #false, it should instead throw an error (i.e., call the function error with an appropriate error string). Avoid unnecessary recursion.

  • Design the function set-false that consumes a list of Booleans and an index n, and returns the list with the n-th item in the list (counting from 0) converted from #true to #false; if the old value was not #true, it should instead throw an error (i.e., call the function error with an appropriate error string). Avoid unnecessary recursion.

  • Design the function draw-map that consumes a list of Booleans and a number n, and displays the list of Booleans as a series of contiguous rectangles who’s width:height proportions are n:1. So, if you decide to make your rectangles 10 pixels in height, and you were called with n=4, each of your rectangles should be 4*10=40 pixels in width. You should render the #true values as black filled boxes, and #false values as black-outlined white boxes.

Exercise 3 Beginnings of a User Interface:
  • Design the big-bang program bit-bucket that consumes a list of Booleans representing a starting list of values, creates a user interface that allows the user to modify some of the values, and produces a list of Booleans representing the final list of values when the user exits. It should also have handlers for key events and mouse events. The world state is up to you.

  • Design the to-draw handler that displays a window with three panels, vertically arrayed:
    • a text box that shows the number of #true values as well as the total number of values in the current list of Booleans.

    • a row of squares representing the values in the list (Hint: maybe you already have a function that can render this?)

    • a message box that shows the message "[THIS SPACE FOR RENT]" by default. (We’ll do more with this box in future phases of this project.)

  • Design the mouse event handler handle-mouse that allows a user to click on one of the rectangles to flip its state, from #true to #false or vice versa.

  • Design the key event handler handle-key that exits the program when the user hits the "Q" key. (Note: it must truly exit the big-bang program.)