### Homework 1

Due Date: Thursday January 16, 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 stirling that takes a natural number n and returns an approximation of n! (n factorial, i.e. the product of all integers from 1 to n), according to the formula attributed to Scottish mathematician James Stirling. This formula is:

The formula uses the well-known mathematical constants pi and e, and the square-root function. All constants and functions you need for this problem are built in to BSL; consult the documentation to find out what they are called.

Since this function computes an approximation of a mathematical value, it is not easy to test it using check-expects. You can omit those for this problem, but make sure to nonetheless run your function on some examples, for small to moderate values of n. Does this formula appear to approximate the value of n! well?

Exercise 2 Design a function area-from-sides that takes three positive numbers as input and computes the area of a triangle formed by three sides of the given lengths if such a triangle exists, i.e. if each of the three numbers is smaller than the sum of the other two. If this so-called triangle inequality is violated, you can return 0.

You can compute the area using Heron’s formula, which is

Since the value of s is needed several times in this formula, you should define a small "helper" function that computes this value for given lengths x,y,z, and then call this function as needed when computing A(x,y,z).

Think about how many tests you need inside your function to check the triangle inequality. You can ignore the triangle inequality in the helper function that computes the value of s, but do not forget signature, purpose statement, and check-expects for both the helper and the area-from-sides function.

Include two check-expects in your solution that demonstrate that your definition recognizes and handles degenerate triangles properly (those that violate the triangle inequality). Also include two check-expects for proper triangles. Since check-expect does not work with irrational return values, you should use triangles that have a rational area. This is the case for right triangles whose side lengths form a Pythagorean triple: numbers a,b,c such that a2+b2=c2. An example is (a,b,c)=(3,4,5). How can you find more such examples, and what is the area of a triangle with such sides? Do some research, and create several check-expects.

Exercise 3 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 4 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 nise 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:

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 5 Design a simple bouncing-ball animation. The animation should take place in a 300x300 scene with white background. A small ball (a black circle of radius 5) is initially located at position (0,150). The ball should now move straight south-east until it hits the bottom edge of the scene. At this time it should bounce off the edge toward the north-east until it hits the right edge of the frame (which will happen at position (300,150)). At this point the ball should turn red and stay at position (300,150) for the rest of the animation (as far as the viewer is concerned, this ends the animation).

To achieve this, define constants for the scene and two same-size balls, one black and one red. For the animation, you need to define a single function, draw-ball, that takes the x-coordinate of the ball (a natural number) as input and draws the ball at the correct position (x,y); you do the math to figure out y. Like pine-tree, this function returns an image. Remember that, for some values of x, the ball moves south-east; for others, it moves north-east. The origin of the coordinate system is the top-left corner! The color of the ball also depends on x.

Once you have defined draw-ball, to start the animation you pass the function name to the animate function: (animate draw-ball). For this to work, your code must include the (require 2htdp/universe) directive (in addition to (require 2htdp/image)).