6.6

Homework 1

home work!

Programming Language BSL

Due Date Friday, September 13 at 6pm

Purpose To write simple functions.

Expectations

For this assignment and all future assignments you must upload one .rkt file in the language specified at the top of the assignment to the Handin Server. Failure to do so will result in a 0. No exceptions. This will be an individual assignment.

Graded Exercises

Exercise 1
  • Copy the following function into your document and give it a signature:
    (define (nonsense a b c)
      (string-append
       (replicate b (substring a 0 (string-length c)))
       " + " c))

  • Confirm this signature by either defining constants or writing a check-expect that passes. If you choose the path of constants you must define ACTUAL-OUTPUT to be the result of calling nonsense on your choice of inputs and you must define EXPECTED-OUTPUT to be the result you expect to be produced by ACTUAL-OUTPUT. You should run your code to ensure that these two constants have the same value. As this code is nonsense, do not provide a purpose statement. For functions with which you aren’t familiar, refer to the DrRacket documentation.

Exercise 2 Define the function self-replicate which replicates a string n times, where n is the length of the string. You will find the replicate function useful. Be sure to provide a signature, purpose statement, and either constants or check-expect statements to demonstrate that the function works as expected for several examples (including the empty string, "").

Exercise 3 Copy the following expression into your file and provide a single-line comment, translating the code into English.

(+ 8 (sqrt 16) (string-length "foo bar"))

For example, given the code (+ 4 (image-height MOON)) you should leave the following in your file:
(+ 4 (image-height MOON))
; four plus the height of the image MOON

Exercise 4 Read about arithmetic sequences here. Define the function arithsum, which computes the sum of an arithmetic sequence given the number of terms, the first number, and the common difference. For example, (arithsum 10 1 3) should produce the number 145. Be sure to include a signature and purpose statement.

Exercise 5 An acre is 43,560 square feet. Let’s assume land is valued at $2,000 per acre (typical for farmland). Assume you have two square fields, each with its own side length (in feet). Define a function which, given the side lengths of the fields, computes the value of the combined land, rounded to the nearest dollar. For example, a square field with side length of 1000 feet is about 22.96 acres, whereas a side length of 900 is about 18.6 acres. Combined, the value of the land in these two fields is $83,104. You should make appropriate use of constants and functions to avoid duplicating code. Remember to supply signatures and purpose statements, where appropriate. The function round will be useful in this problem.

Exercise 6 For each part of this problem, write the code to define a function based upon the supplied signature and purpose statement. Then, copy the supplied tests to your file - they should pass.

a.
; former : String String -> String
; Returns the first supplied string
(check-expect (former "a" "b") "a")
(check-expect (former "foo" "bar") "foo")

b.
; latter : String String -> String
; Returns the second supplied string
(check-expect (latter "a" "b") "b")
(check-expect (latter "foo" "bar") "bar")

c.
; nope : String String -> String
; Returns "baz" no matter the input strings
(check-expect (nope "a" "b") "baz")
(check-expect (nope "foo" "bar") "baz")

Exercise 7 Define the function teleprompter that, when supplied to animate, moves a block of white text (defined via a constant TOREAD) on a black screen upwards. Be sure to include a signature. An example can be seen here. You will need to use the text function, as well as have line breaks in the string you give to the text function. To put a line break in a string in BSL, we use the escape character \ followed by an n (which is short for new line). For example, "line 1\nline 2". Run (text "line 1\nline 2" 24 "olive") in the interactions window to see how this works.