Homework Assignment 1

Expressions in Racket


CS 3540
Programming Languages and Paradigms
Spring Semester 2018


Due: Tuesday, January 16, at 12:30 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 syllabus and elsewhere, Dr. Racket is our default environment for the course.

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. Any time 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 from Session 1. 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, 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 procedures:

Feel free, if you'd like, to explore the chapter on Data in the Racket language reference, which talks about data types and describes basic procedures for operating on each. However, you won't need any information about them that isn't available on this page or in Session 2.

Note that I am not asking you to write Racket functions -- only simple expressions. For example, if I ask you to write an expression to compute the value of 2 + 2, you would write the Scheme 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?
         (* (- 34 27) 11)
         (- 12 (- (- 7 11) 3))
         (/ 42 (* 24 5))
         (+ (* 0.01 20) (/ 4 -3))
    

  2. Create Racket expressions to compute the values of:
         3 * 9 - 12 + 8
         (3 * 9) - (12 + 8)
         1.6  +  6  *  1/3  -  -1.7
         1.6  +  6  *  (1/3  -  -1.7)
    

  3. Give an expression to compute the value of either of the roots of this equation:
         6x2 + 5x - 4 = 0
    

    Yes, you need to use the quadratic formula!


  4. If we evaluate the following definitions in the order given...
         (define big-number 1059034001)
         (define small-number 0.000002005)
         (define ball-state 'cardinals)
         (define number1 big-number)
         (define number2 'big-number)
         (define symbol1 ball-state)
         (define symbol2 'ball-state)
    
  5. ... what is the value of each of these expressions?

         big-number
         small-number
         'big-number
         ball-state
         'ball-state
         number1
         number2
         'number1
         symbol1
         symbol2
         'symbol2
    


  6. 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))
    

  7. 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) x 5) (6 7 8))
         (((1 ((x)) ((3 4) 5))))
    

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

  9. Compute by hand the length of each of the lists in Exercise 6. Then confirm them in an Dr. Racket interaction.


Deliverables

By the due date and time:

Be sure that your submission follows all homework submission requirements. Your file is not a Racket program, so it does not need to have a header block or be loadable by Dr. Racket. (See the requirements page for more about these.)



Eugene Wallingford ..... wallingf@cs.uni.edu ..... January 11, 2018