7.5

Homework 2

home work!

Programming Language BSL

Due Date: Friday January 20, 9pm

Purpose To write simple functions.

Expectations

This will be an individual (non-pair) assignment. For this 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 invalidates your submission and will therefore, unfortunately, result in a score of 0.

Unless otherwise stated, all functions you write must be accompanied by a signature and a purpose statement, in the format we studied in class. As for check-expects: follow the individual instructions for each problem.

Exercise 1 Design a function duplicated? that takes a string s as input and returns #t whenever s has the form xx, for some string x. For instance, abab has that form, while aba does not.

Exercise 2 Define a constant ANGLE of value 120. Now design a function pine-tree that takes two numbers a and d as input and returns the image of a simplistic holiday tree (late-season as that may be), as follows. The tree consists of 5 upward-pointing isosceles triangles (of color green, obviously—forestgreen is nice and is a valid BSL color) stacked on top of each other. The top triangle’s equal-length sides have length a and are joined at an angle of ANGLE degrees. For the triangle below that, the equal-length side length increases to a+d, and so forth, with the bottom triangle having equal-length side length a+4d. The angle does not change. The stack of triangles sits on top of a brown rectangle of width a and height 1.5a, depicting the trunk of the tree.

Here is an example:

A pine tree

The return type of your function will be Image (useful to know for the signature). Remember to include (require 2htdp/image) in your code, to enable the picture-drawing features.

You can omit check-expects for this function. Feel free to experiment with the branch-angle parameters used in this problem, to see whether other values give a better impression of a pine tree. Are there other parameters that are hard-coded into your function definition and should really be defined as constants outside?

Exercise 3 Design the function subset-interval? that takes four real numbers a,b,c,d interpreted to represent two closed real intervals [a,b] and [c,d], and returns #t exactly when the first is a subset of the second. That is, your function returns a Boolean (as also suggested by the ’?’ in the name). "Subset" means that the first interval is nested inside the second. That is, every element of the first interval is also an element of the second interval. In this case your function should return #t. It should return #f in all other cases.

Note that when a > b (which is a valid input), the interval [a,b] is considered "degenerate" and empty. In contrast, when a = b, the interval is non-empty and contains exactly one number (namely, a). Make sure your check-expects test such degenerate cases, and also boundary conditions, such as when the two intervals have the same left boundary: a = c.

Exercise 4 Implication, denoted => , is probably the most frequently mis- understood binary Boolean connective! It is formally defined by the following truth table, which uses 0 in place of #f and 1 in place of #t.

x | y | x => y
--------------
0 | 0 |   1
0 | 1 |   1
1 | 0 |   0
1 | 1 |   1

Design the function => that takes two Booleans and returns the truth value of the implication connective applied to these two Booleans. That is, the return value is a Boolean, too. For this function you are not allowed to use any Boolean connectives built-in to BSL, such as and, or, or not. You are allowed to use cond, if, Boolean constants, and of course the arguments to the function.

Design check-expects that cover all four cases in the mathematical definition of implication given above. Note what this means: if these tests pass, you have demonstrated the correctness of your implementation of => for all possible inputs.

So, by your implementation, is the following statement true or false?

"If Boston is the capital of the moon, there will be two exams in Fundies I."