On this page:
1 Install the Handin plugin
2 Defining constants (variables)
3 Defining functions
8.14

Lecture 2: Definitions🔗

This assignment is due on Tuesday, August 27 at 11:59pm. Submit it using Handin as assignment lecture2. You only need to submit the first 6 exercises. Your submission is only accepted if the message “Handin successful” appears.

1 Install the Handin plugin🔗

All lecture assignments and problem sets will be submitted through the Handin server. To use Handin in DrRacket, you’ll need to install a plugin in your DrRacket. Follow the instructions on this page to install Handin.

2 Defining constants (variables)🔗

Exercise 1. Copy an image from the Internet, and use the 2htdp/image library to “frame it inside itself”. For example, if the image you downloaded looks like this:

then make an image that looks like this:

appear in the Interactions Window when you hit the Run button. The inner image should be 70% as wide as and 70% as tall as the original image.

Hint: use scale and overlay.

Hint: if you’re looking for an image to use, try this one.

Introduce a constant definition for the original image you downloaded, so that the Definitions Window contains only a single copy of a single image.

Now is a good time to submit your work so far, using Handin as assignment lecture2 (not ps1 or lab1 or test-handin). Throughout this course, it is a good idea to submit early and submit often, because
  • You can submit any assignment any number of times until the deadline. (We do not accept late submissions.)

  • In case you make a mistake, Handin sometimes provides helpful messages.

  • In case you lose your work, you can retrieve your submission from Handin.

  • We only grade your last successful submission. (Moreover, only your Definitions Window is submitted, so each time you submit, your Definitions Window should include all your work so far on that assignment.)

Your submission is only accepted if the message “Handin successful” appears.

Troubleshooting If you encounter any trouble, one way to get help is to post a screenshot to Discord.

Troubleshooting If Handin says “file to handin is too big”, it means that your submission cannot be accepted because it exceeds the maximum upload size: 7 MB. Be sure that any image that you’ve included in your file is small enough (you may need to rescale it in an image editor).

Exercise 2. Copy a different image from the Internet, and change your constant definition in Exercise 1 to it. Now when you hit the Run button, the new image should be framed in the same way by the same framing formula.

Exercise 3. What’s wrong with this program?
(define n (+ 3 2))
(* (n) (n))
And what’s wrong with this program?
(define (n) (+ 3 2))
(* n n)

The code written in the videos above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

3 Defining functions🔗

Exercise 4. Let’s keep framing the same image, but make it easier to adjust the size of the inner image. Define a function called framed that takes a number (like 0.7) as input and produces a framed image like the ones we’ve created before. The outer image and the inner image will be from the same picture, and the inner width and height use the given number as the proportion (like 70%) of the original. For example, after you hit the Run button, typing (framed 0.5) and (framed 0.7) and (framed 0.9) in the Interactions Window should produce framed images like the following, except the fruit is different:

Put differently, define a function with the following signature:
; framed : Number -> Image
; frame the image scaled by the given number
(define (framed n)
  FILL-IN-THIS-BLANK)

Exercise 5. Define a second function, called shrink:
; shrink : Number -> Image
; make a smaller version of our image
; (shrink 5) should produce (framed .9509900499)
(define (shrink n)
  (framed (expt 0.99 n)))
This function takes a number n (like 5) as input and produces a framed image whose inner width and height are 0.99n of the original.

Try to use this new function shrink in the Interactions Window.

Pay attention to how this new shrink function uses your existing function framed. In other words, the definition of shrink contains (framed ...). The input to framed is the proportion (expt 0.99 n); this sub-expression uses the built-in function expt for exponentiation.

Optional: Review operators and definitions.

The code written in the videos above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

Exercise 6. Use our function shrink to make an animation:
(require 2htdp/universe)
(animate shrink)

Optional: Enjoy more examples of animate.

For the rest of this page, you don’t need to submit anything, but do watch the videos and try the exercises anyway. We’ll work through similar material together in class.

When you do the lectures, make sure you do the whole lecture, not just the required parts. Make sure you understand WHY a function works, not just the code

The code written in the video above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

Exercise 7. Show the calculation of (shrink 50) step-by-step. Use the Stepper to confirm that your steps are correct.

Optional: Read Chapter 2 of the textbook.

Optional: Learn a little more about DrRacket.