6.10

Assignment 7a

home work!

Programming Language ISL

Due Date: Monday at 9:00pm (Week 7)

Purpose To practice designing functions on lists, and extending and refining earlier programs.

Finger Exercises

Exercise 1 (Warmup) Consider the following data definition:

(define-struct small-shelled-crab [name color disposition size])
(define-struct large-shelled-crab [name color disposition size])
 
; A HermitCrab is one of:
; - (make-small-shelled-crab String String String PositiveNumber)
; - (make-large-shelled-crab String String String PositiveNumber)
; Interpretation:  A hermit crab either has a small shell or a large shell.  
; Each crab has a name (e.g., "Frank"), a color (e.g, "black"), a
; disposition (e.g., "friendly"), and a size in millimeters

Hermit crabs grow over time, and are also known to change shells. Specifically, if a hermit crab grows larger than 14 mm, it must find a large shell (as it will no longer fit in a small shell).

Design the function grow-crab, which accepts a HermitCrab and returns a HermitCrab that has grown by 1 mm.

Exercise 2 (Trickier) Complete the exercises from the lab on re-implementing Finite State Machines. For corresponding exercises from the book, work through section 12.8, and complete HtDP Exercise 230. The data definitions there are slightly different than the ones from lab. Use the finite state machine from Assignment 4a.

Graded Exercises

Today’s assignment is entirely about the Forum project. It’s stated in terms of three separate stages that build on one another. You will only submit a single file, with all three stages completed.

Updates:
  • I was under the impression these functions were available to you in BSL or ISL by default, but they are not...

    You may want to include Racket’s string library, which may contain some functions of use to you:

    (require racket/string)

  • In class we have upgraded from BSL to ISL. You may do the same on this and future assignments. Do so by going to the Language menu -> Choose Language, and select "Intermediate Student".

In Assignment 5a we asked you to design the world program simple-net-forum. Before proceeding, fix your solution in response to our feedback.

As we mentioned back in in Assignment 3b, using images to represent your data is a violation of the design principle of separating application logic from the user interface — and even though it could suffice through the last problem set, it will not suffice any longer. HtDP refers to this principle as the Model-View-Controller or MVC design pattern. Luckily, we now have lists, which support an appropriate means for representing a sequence of posts.

All of your changes this week are client-side only, meaning that whatever messages you send to the server, and whatever messages you receive from the server, are completely unchanged. You will only be modifying how to draw the user interface, how to handle keyboard input, and how to represent your world.

First, update your Forum representation to use lists as a means of representing the previous posts. Your data definition should no longer contain images. If you’ve already switched to using lists then this step is done!

Stage 1 – New world state definitions: We want to make the program a bit more Piazza-like. To that end we have determined two main things that are useful: creating and viewing messages, and searching through the history of messages.

To help you out with implementing this functionality we have provided a partial data definition below. You will need to complete the design of this data in order to continue with the project. If you would like to support additional functionality you can make changes to this definition in order to do so. Understanding this data definition, and the purpose of its pieces, is critical to getting the rest of the assignment to work, so take the time to work through these definitions, write down their associated templates, and build examples of each.

; A World is one of
; - EditviewPosts
; - SearchPosts
; INTERPRETATION: Represents two different "views" in your program.
 
; A EditviewPosts is a (make-editview Edit History Search)
; INTERPRETATION: Means the user is viewing all posts and
; potentially typing in a new one.
; A SearchPosts   is a (make-search Edit History Search)
; INTERPRETATION: Means the user is trying to view only a subset
; of the existing messages.
 
; A Edit is a String
; INTERPRETATION: the contents of the post the user is currently
; editing.
; A History is a List of Strings
; INTERPRETATION: the prior posts received from the server.
; A Search is a .........??? (for you to complete!)
; INTERPRETATION: the current search term the user is looking for

Note that both (make-editview ...) and (make-search ...) contain the same three fields. This is deliberate: as the user switches between editing a new post and searching through old posts, your program shouldn’t lose track of the earlier history, or of whatever partial post the user has typed, or whatever partial search the user has typed. Additionally: the (make-editview ...) is very similar in interpretation to the existing world states you had in prior assignments. You ought to be able to reuse your existing functions almost unchanged (you might need to rename your structure definitions slightly), as helper functions for this current assignment.

An Edit needs to keep track of what post the user is currently editing. A History needs to keep track of the prior posts received from the server. You may notice that we have not defined a Search completely. We leave that to you. A Search needs to keep track of whatever text the user is currently searching for. (Hint: this is pretty similar to an Edit, but has a different interpretation.)

Revise your program to use these new data definitions. You are encouraged to use stubs and templates frequently, to figure out how to incorporate your existing code into this new setup.

At this point, if you start your program with a world in EditviewPosts-mode, it should behave identically to simple-net-forum.

Stage 2 – New functionality: Searching In addition to all the functionality from Assignment 5a, you should be able to search for a post. Searching for the string "cat" should return any post that contains the word "cat" anywhere in the post. Searching should be incremental: for instance, if the user types the letter "a", then your search should display all posts containing the letter "a". As soon as the user types the letter "n", the display should show only those posts containing "an". If the user presses backspace, the display should show posts containing "a" again. You may ignore any invalid characters (like Enter, or the arrow keys, etc.).

Hint: you’ve likely already designed a function that can help you search for strings...

Suggestion: Your data definition includes the entire history of posts, but in SearchPosts-mode, you only want to display a subset of those posts. It is probably easiest for you to implement this "display only the posts that match" filtering in the to-draw handler for your SearchPosts mode. If you already have a helper function for displaying a list of posts, then this should be a very small modification to your code.

At this point, if you start your program in SearchPosts-mode, it ought to allow you to search for posts, but it won’t allow you to edit new posts. But you don’t currently have a way to switch between modes.

Stage 3 – New functionality: switching modes Now that you have a new way to handle searching for strings in the history of posts, you need a way to switch between EditviewPosts-mode and SearchPosts-mode. Use the function key F1 to switch to EditviewPosts mode, and F2 to switch to SearchPosts mode. (Your key handler will receive these as strings "f1" and "f2" respectively.)

Suggestion: You should design a new key handler, that handles these two new keys, and for everything else, either calls your existing key handler (if you’re in EditviewPosts mode), or calls another new handler (for dealing with SearchPosts mode). This way you can design and test each function independently, so you can handle the mode-switches in exactly one place, and so that bugs in your keyboard handler for one mode do not affect the other mode.

At this point, you should have integrated editing and searching functionality into a working program.

As always, you are welcome to implement additional functionality should you desire to do so.