Intro to Racket and Composing Functions Solution



The Assignment


  1. Start up DrRacket. If this is the first time you’ve brought up DrRacket, you will be asked to choose a language. From the Language menu, choose Choose Language. From How to Design Programs, select Beginning Student. If a Show Details button appears in the bottom left-hand corner of the window, click it. On the right-hand side of the window look at the panel that says Output Syntax. Under Constant Style choose true false empty. Click the OK button. Now, in the definitions window, write a comment that includes your name and your username (and those of your partner, if you’re working with a homework partner). From the File menu, choose Save Definitions As…. Name your file according to the naming conventions for homework files. Click the Run button.

NOTE: DrRacket saves the work done in the Definitions window only.

As you answer each of the remaining problems, use comments in your file to clearly indicate the number of the problem you are solving, e.g..

;;  Problem 1a

Expression Evaluation

  1. Evaluate each of the following expressions. Show every step in the evaluation. Provide your answers as comments in the Definitions window. For example, if the problem asked you to evaluate the expression

(* (+ 4 3) 9)

you should provide the following comments in the Definitions window as your answer:

;; (* (+ 4 3) 9)
;; (* 7 9)
;; 63

(You may use Racket’s “comment out with a box” feature instead of using semi-colons if you prefer.)

    • (a.)

(cond [(> (string-length "dam") (string-length "river"))
         (+ (* 19 4) 62)]
      [(string=? (string-append "a" "bc") (substring "abcde" 0 3))
       (- 520 (- 15 4))]
      [else 1231])
    • (b.)

(if (> (add3 3) (+ 5 2))
    (add3 (+ 6 (- 19 4)))
    (* 7 (add3 5)))

where add3 is defined as

(define (add3 n)
   (+ n 3))

Composing Functions

Provide a signature and purpose for each function you write.

  1. In class and in the videos you were introduced to some built-in functions (like rectangle and text) from the image library. Add the following line to the Definitions window:

(require 2htdp/image)

Now click the Run button. The functions in the image library are now available to your program.

From the Help menu, choose Help Desk. A browser window will open. Under the Teaching heading, choose How to Design ProgramsTeachpacks. In the left margin, click on HtDP/2e Teachpacks. Here you will find a description of all the pre-defined functions in the library. Notice the …search manuals… box at the top of the page. Type in image-width to find information about that function. When the DrRacket Helpdesk provides the matches for image-width, choose the one from “2htdp/image”.

Develop a function called four-square that consumes an image and produces an image that consists of 4 copies of the original image, as shown in this example:

if the original image looks like this: 

then the produced image should look like this: 

Use the DrRacket help desk to learn about the following functions (available in the 2htdp/image library) that you might consider using for this problem:

    • flip-vertical

    • flip-horizontal

    • beside

    • above

(You’re not restricted to using these functions. You may solve the problem any way you want as long as the image is duplicated in the same manner as the image in the illustration.)

You won’t be using check-expect to test this function, but you still want to be reasonably sure that it works correctly. To convince yourself that the function works, begin by copying two images into the Definitions window. Google images is a good repository for images. Choose square-shaped images (you’ll get funny-looking results if you apply the four-square function to an image that doesn’t have the same length and width). To copy an image, right-click on the image, select Copy, then in the DrRacket Definitions window, select Paste. Use define to give names to the images you copy into your program. Now run your four-square function twice (once with each image).

Note: Don’t forget to provide a signature and purpose for your function. You do not have to test this problem with check-expect.

  1. One of the big ideas we’ll encounter again and again this term is that we can take the value returned by one function and use it as an argument to another function. Let’s try it with an example.

Assume that a person’s income tax is based on their annual income, their tax bracket (a percentage), and the number of dependents they claim (a person may claim 0 or more dependents). The amount of income tax owed is calculated by multiplying a person’s income by their tax bracket, then deducting $1000 for every dependent claimed. For example, if a taxpayer is in the 25% tax bracket and they claim 3 dependents, that person’s income tax will be calculated by multiplying their annual income by 0.25, then subtracting $3000. If the calculation results in a negative number, a value of 0 should be produced.

A person’s tax bracket is based on that person’s annual income and marital status (either single or married). Here are the tax brackets for a single person:



$0 – $14,999


$15,000 – $49,999


$50,000 – $99,999


$100,000 or more


and here are the brackets for a married person:



$0 – $31,999


$32,000 – $129,999


$130,000 – $224,999


$225,000 or more


Do the following:

    • (a) Develop a function named bracket that consumes a person’s annual income and their marital status (a boolean that’s true if the person is single and false if they’re married), and produces a percentage (either 0.10, 0.20, 0.25, or 0.35, as described above).

    • (b) Develop a function named tax that consumes a person’s annual income, their marital status, and the number of dependents, and produces the amount of income tax owed by that person. The tax function must call the bracket function.

Use defined constants where appropriate. Make sure each function you define is documented with a signature and a purpose. Each function should be tested using check-expectNOTE: You must name each function with the exact name specified in the problem. Your signature must conform to the problem description. Otherwise, we won’t be able to run our automated tester on your program, and you’ll lose points. Programs that don’t work with our auto-tester (and thus must be tested manually) will be penalized with a deduction of 10% of the total number of points for the assignment.

Guidance on testing: Test bracket thoroughly (i.e. test both single and married persons at different income levels; don’t forget to test boundary conditions). Provided that bracket has been tested thoroughly, you will not have to include tests for every combination of marital status and income level when you test tax.


Here is the grading rubric the graders will use for Homework 0.

What to Turn In

Using InstructAssist, choose Tools
-> File Submission
 to turn in the file (with the .rkt extension) containing your Definitions Window content. The name of the project in InstructAssist is Homework 0. If you submit your program to the wrong project in InstructAssist, your submission will be penalized with a deduction of 10% of the total number of points for the assignment.

Your name and your wpi login name must be listed in a comment at the beginning of your file. Programs submitted after 11:59pm on Tuesday, January 15 will be considered late. No submissions will be accepted after 11:59pm on Wednesday, Jan 16.

If you submit a program that doesn’t run under DrRacket, the grader will notify you by email and give you 24 hours to resubmit. The re-submission will be penalized with a deduction of 25% of the total number of points for the assignment.