Homework 1:
Expressions in Racket

Due: Monday, January 22, at 11:59 PM

Introduction

This assignment gives you your first chance this semester to write your own expressions and use a Racket interpreter to evaluate them. As mentioned in the the syllabus and elsewhere, Dr. Racket is our default environment for the course. You may use the command-line interpreter, if you prefer.

Learning a new programming language involves learning its tools. Being an interactive language, Racket's tools may feel different than the programming tools that you are used to using. Be sure to devote sufficient time and energy to familiarizing yourself with Dr. Racket, its editor, its help system, and the like. Time invested now will pay big dividends later on!

As we go through the course, you will occasionally run across a question of the form, "I wonder how I do <x> in Dr. Racket?" In order to support your quest for timely answers, I maintain a page of helpful hints for using Dr. Racket. If someone asks a question that seems to have some long-term use to us, I will add the question and answer to the page.

The Assignment

Open Dr. Racket. Hide the Definitions pane, using the option available on the View menu. Familiarize yourself with the Interactions pane by typing a few simple expressions, perhaps riffing the expressions we evaluated in Session 2. Any time you want to "clear" the interactions, hit the Run button at the top of the window.

Once you feel reasonably comfortable with Interactions pane, do the tasks below. For each task, you will...

When you are done, save the contents of your Interactions pane to a text file, using Save Interactions as Text... option that is available under Save Other on the File menu. You will submit this transcript as your homework assignment.

You may want to use the following Racket primitive functions:

You do not need any information about Racket's data types that isn't available on this page or in Session 2. After you finish the assignment, you are certainly free to explore Chapter 4 in the Racket language reference, to learn more about the many data types available in the language and the basic functions for operating on each of them.

Note: I am not asking you to write Racket functions -- only expressions. For example, if I ask you to write an expression to compute the value of 2 + 2, you would write the Racket expression (+ 2 2):

> (+ 2 2)
4
> 

We will begin writing functions soon enough. Be patient!

Some New Racket.    Exercises 5 through 8 deal with Racket lists. You all have experience with lists in Python. Here is a quick introduction to Racket lists. (We will learn more about lists next week.)

You are ready to begin!

Exercises

  1. What is the value of each of these expressions?
    (+ 14 (- 39 27))
    (- 73 (* (- -7 11) 3))
    (* 30 (/ 24/30 7))
    (+ (- (* 10 98) (* 7 6)) (* 543 2 1))    ; Happy New Year!
    

  2. Create Racket expressions to compute the values of these arithmetic expressions, and evaluate your Racket expressions.
    4 * 7 - 11 + 6
    (4 * (7 - 11) + 6)
    1.6  +  1/3  *  6  -  -1.7
    1.6  +  1/3  *  (6  -  -1.7)
    
    For the unparenthesized expressions, use the precedence rules you know from Python or Java.

  3. Give an expression to compute the value of either of the roots of this equation:
    3x2 + x - 24 = 0
    
    Yes, you need to use the quadratic formula!

  4. Evaluate the following definitions in the order given:
    (define big-number 1059430001)
    (define small-number 0.002000015)
    (define msu 'spartans)
    (define number1 big-number)
    (define number2 'big-number)
    (define symbol1 msu)
    (define symbol2 'msu)
    
    What is the value of each of these expressions?
    big-number
    small-number
    'big-number
    msu
    'msu
    number1
    number2
    'number1
    symbol1
    symbol2
    'symbol2
    

  5. For each of a, b, c, and d, give an expression using only calls to first and rest that returns that value when applied to this expression:
    '(a (b  c) d)
    

  6. For each of these expressions, give an expression using only calls to first and rest that returns x when applied to it:
    '(5 4 (x) 2 1)
    '(1 (2 (3) 4 5) (6 x 8))
    '(((1 ((2)) ((x (3)) 5))))
    

  7. Construct a single list that makes the following interaction work:
    > (define x /your answer/)
    > (first (rest x))
    '(a b)
    > (rest (first x))
    '(c (d e))
    > (rest (rest x))
    '((f g) h)
    

  8. What do you think the length of each of the lists in Exercise 6 is? Check your answers in a Dr. Racket interaction using the length function.

Transcript

Create a transcript of your interaction with Dr. Racket that demonstrates your expressions for each of the above exercises.

Deliverables

There are two deliverables for this assignment.

First: By the due date and time, submit the transcript of your interaction with Dr. Racket using the submission system.

Second: By the due date and time, email me at least one new question that you have about Racket now that you've worked with it for a while. This is your chance to have me answer the questions that are slowing you down right now. (Yes, this is required.)