Articles, Blog

racket definitions video 4/4 —- define function, by design

December 3, 2019


Here’s Doctor Racket! You’ll require picturing-programs. It provides, among other things, some
examples of images. Click Run, and we’ll see all’s well, and what those images
look like. Now hide the interactions pane, so we’re
working just in definitions… We are going to define a function rosette in a more systematic
way and we did last time. So, first we start off with a contract… input and output both images in this
case. Next, a purpose statement. these lines that begin with semicolons
are ignored by Doctor Racket… but they’re really important to human
beings who are reading and planning programs. First, let’s check what we expect rosette to do. Here’s an example. What do we expect rosette do do with the
image of hacker? Well, it’s going to have to put two flipped versions of hackers side by side. So one
flipped version puts hacker above its vertically flipped self… And that’s how we do that. That’s enough typing that we should
probably copy it for later use. So, what we’re going to do is put that
beside it’s horizontally-flipped self. There, that completes our expected version of (rosetta pic:hacker) What do we expect rosette to do with the
calendar image? Well, here’s what we expect: something very
similar to what it did with the hacker image, except calendar’s gonna
appear everywhere that hacker occurred before. So, once again we’re putting the
vertically flipped pair of calendars beside a horizontally flipped version of
themself. I can put that on another line… Dr Racket will just indent it
dr racquetball discontented where it takes we mean it to be indented. Now, I define the function header… …key word define… left paren… the name of the function and any placeholders, or parameters I’ll need. The function’s name is rosette. We’re gonna use the pic placeholder everywhere we have an image. So, it would stand for calendar, if we were applying rosette to calendar. It would stand for hacker if we were
applying rosette to hacker. To start with, we’ll just do a stub; we’ll do
something totally stupid and useless that couldn’t possibly be what we expected
just to get our foot in the door. So, if I run this… not completely surprisingly… it doesn’t work; it reports that I failed all the tests. It does better than that, it tells me how I failed. It says the actual value, that
empty circle is different from what was expected, that 2×2 array of hackers. The second test failed for similar reasons:
the actual value doesn’t match 2×2 array of
calendars. So, we’ll go back and try to fix this.
Again, we’ll work just in the definitions pane, and we get some inspiration by looking at our
examples. They’re very similar form of instructions. The only thing that
really changes: the actual images that are provided. So, we can emulate the approach in our
examples to create the function. Same sequence of instructions, except
everywhere that hacker or calendar appeared we put our
placeholder, pic. Fast to type, because pic’s nice and short too. Done. so now we run this… And it reports that we passed tests; that we are in fact correctly producing the rosette of hackers and the rosette of
calendars. yippee isn’t legal, but it’s fun to type sometimes. Let’s review this recipe for functions. We need a contract. We need a statement of purpose(s). We need to state our expectations using
check-expect. We need to define a function header… that defines the name of the function, rosette
in this case, and any parameters we’ll need as
placeholders. Finally, we need a function body. It should either work or at least be a
stub, to hold the place for something that does work.

No Comments

Leave a Reply