Introduction to Systematic Programming – Part 4
Join the DZone community and get the full member experience.Join For Free
originally authored by
in the event that you're just joining in, here's a guide to the rest of this series:
like i said when i was writing week 3’s post , week 4 was crazy complicated. however, i will candidly say that since i had a feeling the week was going to be harder, i really did spend more time walking through the coding of these new systems and i think that made a huge difference, because on week 3’s quiz i got a 7.50/11, and in week 4 i achieved 9.7/10. so, take that how you will!
lesson one: interactive programs
as for the focus of week 4, the subject matter is the how to design worlds recipe, as well as an introduction into compound data. this means we started to learn the functionality needed to build things like animations, games, and, eventually, desktop apps, as well as how to utilize basic graphics, as always, all in dr. racket.
lesson two: the big bang mechanism
as expected, the “big bang” mechanism is an amusing reference to the creation of the actual world, and it’s used in dr. racket as the “mechanism that supports interactive programs” – in actual usable terms, big bang is an expression that wires 2 or more functions together, to create a world state. this lesson encourages the students to watch some programs and try to decipher how they might work, and what sorts of functions they might need. it then goes on to explain that programs manage things that *change* and if your program doesn’t change anything, it wouldn’t be very useful, now, would it?
here’s the initial example of big bang that we’re given in class, along with some notes:
(big-bang 0 ;; cat
(on-tick next-cat) ;; cat-> cat
(to-draw render-cat)) ;; cat->image
the first line of big-bang evaluates to create the initial world state. after that, there are different functionalites of big-bang, those that will be wired together, explained inside the ()s of big-bang. the way the instructor explains this, again, is that it “squeezes” the functions together to produce the world you’re designing inside.
you’ll see ;; notes next to the function names – these are called “function signatures” and they help the programmer to flesh out how these big-bang abilities will be built into functions later in the program.
extra notes from this lesson:
- place-image is a function that puts an image at designated x,y coordinates.
- mts displays a new screen
lesson three: how to design worlds analysis
as with every other part of introduction to systematic programming, there is a recipe for how to create worlds, known affectionately as “how to design worlds” or htdw. there are two phases to this recipe, and lesson three walks us through the first portion, domain analysis.
domain analysis has 4 steps:
1. sketch program scenarios
2. identify constant information
- these are the things that do not change through the course of your program
- try to identify as many of these as you can during this phase of domain analysis; however, if you think of one later, it’s totally ok to add it in at that time!
3. identify changing information
- these are the things that do change through the course of your program.
4. identify big-bang options
- there’s a handy table in your class references to assist with this:
if your program needs to:
then it needs this option:
change as time goes by (nearly all do)
display something (nearly all do)
change in response to key presses
change in response to mouse activity
extra notes from this lesson:
templates are not only for beginners! they should be considered an expert’s way to outline the basic structure of a complex problem before diving into the details! they allow the designer to consider the question, “given that i’m using this structure, what do i know is “known” to be true?” and thus break down their problem.
lesson four: how to design worlds – program through main function
the second step in designing a world after domain analysis, is building your program! there are also four steps to consider here.
1. constants (base this on 1.2 from lesson 3)
a. consider tracability between the code/analysis. this means that your domain analysis should be easily referable so that you can complete your coding! if you have a constant in your analysis, you should have it in your program and you should be able to go back and figure out which is which, easily!
2. data definitions (base this on 1.3 from lesson 3)
a. for this item, we would use our typical data definitions recipes that we learned previously in the course, and refer to the appropriate references.
a. main function (based on 1.2 and 1.3 from lesson 3)
b. wishlist entries
i. a wishlist entry is the stub of a function that you need to create in order to get the program to run! this is based on 1.4 from lesson 3, in which we determined which big bang options we needed to utilize for our world to act in the way we desire. a wishlist entry should be made for each of these options so that we remember to go back and code them in step 4!
lesson five: how to design worlds – working through the wishlist
4. work until you’re done!
a. code. code. code. code! (oh, and test!)
it’s noted in this section to make sure that you work systematically!! don’t jump around like a crazy person. the htdf/htdw method and recipes are structured to assist you with working through your program in a systematic fashion, focusing on one thing at a time until you’re done. this lets you break up the difficult aspects of a complex problem into smaller, more managable chunks.
lesson six: improving a world program – add speed!
the most important takeaway from lesson six is to always remember that the domain analysis is simply a model of the program you’re working to create. if you want to add on to your program, you can use that model to plan out your program/changes you want to implement.
lesson seven: improving a world program – add a key handler
key handlers in dr. racket are handled by the big bang option, “on-key”. these key events are of the primitive datatype and constitute the first “large enumeration” that we are seeing in this class. large enumerations are exactly what they sound like, having a list of specific values for a specific list of items. when using a large enumeration, it’s important to remember to “white box test”. as we know, typically for an enumeration, we would write a test for each item on the list. however, for a large enumeration, this is not practical, so we would want to test a correct response, a wrong response, as well as an equivalent response, to cover all potential instances of response that the function might produce, as opposed to testing each item one by one.
to create these key event functions, the template used is a cond, with a q/a pair for each special case. for example, if you’re looking to answer a yes or no question with “y” and “n”, you would write a cond for y and x, and then finish with an “else” clause, because the rest of the enumeration list would produce the same results!
lesson eight: define struct
i was really excited when i saw this lesson title, because i learned about structs in my last class, cs50x on edx, so i had a feeling i would have a bit of an advantage when watching this session… and i was right! thank goodness, because i was getting kind of overwhelmed this week.
so ok, what is a struct?
a struct is a compound datatype that takes two or more values that naturally belong together and weaves them into one shiny new piece of data. structs are formed with the following template:
here’s an actual struct example:
(define-struct pos (x y))
in this example, the first name is replaced with pos, (interpreted as position) and the next name(s) given are x and y, which become the field names. these field names can be accessed later in the program by using the selectors, which in this example would be pos-x and pos-y. structs also have a predicate, which in the case of this example would be pos? .
lesson nine: data definitions
complex data, again, is two or more values that naturally belong together. this receipe is very similar to how we typically define data, and utilizes the following five steps:
- determine the possible structure definition
- determine the type comment
- write the interp
- form examples/tests
- template the definition!
week 4 summary
as mentioned this week was a doozy! however, i really appreciated the last lesson in the week, which i didn’t put notes down for because honestly, it was a full-fledged walkthrough/review of everything we’ve learned so far. we were given a dr. racket starter template and got to build the program right along with our instructor. i found this *incredibly* invaluable, even moreso than the homework problems and practice problems. honestly, i think this is because we were able to see the methodology behind the answers, as opposed to just getting the answers like we did for the homework in week 1 of the class. there was one important item that was covered here that i think bears mentioning, though. when writing your program, it’s totally, totally ok to refine what your original plan was while you’re working, as long as you think it through. specifically in dr. racket and this class, we’re encouraged to write functions that only do one thing, so sometimes when i’d initially had one program vision, i may have to revise it based on instructions or the design recipe. that’s ok! we’re taught in the class that this is the difference between a “coder” and a “program designer” – make sure you’re the kind of developer who thinks and truly who designs her programs!
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.