Homework 1:
Expressions in Racket
Due: Monday, January 26, 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 certainly 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 dividends later, when the assignments are more challenging.
As we go through the course, you will occasionally ask yourself 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 you ask a question that's not on the list and would be useful to other students, I will add the question and answer to the page!
The Assignment
Using Dr. Racket
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 on 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...
- write one or more Racket expressions to compute the values described in the problem, and
- evaluate your expressions within the Dr. Racket Interactions pane.
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.
Things to Use and Not to Use
You may want to use the following Racket primitive functions in your expressions:
-
(sqrt x), which returns the square root ofx, and -
(expt x y), which returnsxraised to theypower.
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. Patience, young grasshopper!
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, all you need for Homework 1. (We will learn more about lists next week.)
-
In Racket, we use parenthesized expressions to create
lists, too. For example,
'(1 2 3 4)is a list containing the first four integers. -
The main functions for accessing the parts of a list are
firstandrest. They do just what their names say:firstreturns the first item in a list, andrestreturns the rest of the list after the first item. So:> (define my-list '(1 2 3 4)) > (first my-list) 1 > (rest my-list) '(2 3 4) > (first (rest my-list)) 2 > (rest (rest my-list)) '(3 4)
-
To find the length of a list, use the
lengthfunction:> (length my-list) 4 > (length (rest (rest my-list))) 2
What doeslengthreturn if its argument is not a list? Try it out!
You are ready to begin!
Exercises
-
What is the value of each of these expressions?
(+ 39 (- 27 14)) (- 73 (* (- 11 -7) 3)) (* 30 (/ 4/5 7)) (- (+ 10 (* 98 76)) (* 5432 1)) ; Happy New Year!
-
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. -
Give an expression to compute the value of either of the roots
of this equation:
3x2 - 14x + 15 = 0
Yes, you need to use the quadratic formula! -
Evaluate the following definitions in the order given:
(define big-number 1059430001) (define small-number 0.002000015) (define indiana 'hoosiers) (define number1 big-number) (define number2 'big-number) (define symbol1 indiana) (define symbol2 'indiana)
What is the value of each of these expressions?big-number small-number 'big-number indiana 'indiana number1 number2 'number1 symbol1 symbol2 'symbol2
-
For each of
a,b,c, andd, give an expression using only calls tofirstandrestthat returns that value when applied to this expression:'(a (b c) d)
-
For each of these expressions, give an expression using only
calls to
firstandrestthat returnsxwhen applied to it:'(5 4 (x) 2 1) '(1 (2 (3) 4 5) (6 x 8))
Hint: you can use Dr. Racket to help you find subexpressions. Place your cursor just before a(or just after a), and Dr. Racket will highlight the expression. -
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)
-
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
lengthfunction.
Transcript
Create a transcript of your interaction with Dr. Racket that demonstrates your expressions for each of the above exercises.
-
Create your transcript by saving the contents of your
Interactions pane to a text file, using
Save Interactions as Text.... -
Change the extension of the filename to
txt. - Do not edit the file in any way!
Don't worry. I expect to see mistakes in your interaction file, both from typos and from experiments on the way to a correct answer. This is normal, and I want to see the path you took through the assignment.
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.
- Be sure that your submission follows all homework submission requirements.
- Do not edit your transcript file in any way. Your file is not a Racket program, so it does not need to have a header block or be loadable by Dr. Racket.
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.)