CS1321 Homework Assignment Two

Due Friday, June 1 at 8:00:00am

Any function writen in a homework may be used within the same homework to solve a given problem.
If you need to reuse a solution from a previous homework, you must resubmit the function in the current homework.
Be sure to follow the guidelines as outlines by the homework template and style guidelines documents.

Objectives

Problem 1
10.0 points

In probability and statistics (as well as combinatorics) you occasionally want to see how many ways you can choose r elements from a set of n elements. The formula (given algebraically) is

nCr(n,r) =     n!
           ----------
            r!(n-r)!

Write a Scheme function named nCr which will take in two parameters: n and r (in that order) and will return the answer as given by the above formula (remember, "n!" means "the factorial of n").

HINT: Failure to decompose the problem and use good abstraction will lead to a poor grade.

 

Problem 2
20.0 points

Imagine there is a town with a complicated set of rules for who can be served alcohol. The rules are numbered and printed on signs which are then distributed to bars. The rules look like this:

  1. No person under the age of 18 may be served alcohol.
  2. No person may be served alcohol on a Sunday.
  3. No person who's age is a prime number may be served alcohol.
  4. All persons over the age of 21 may be served alcohol.
  5. Persons in the militia may be served alcohol if they are 20 or older OR if it's a Friday.
  6. All persons who's age is evenly divisible by 6 may be served alcohol.
  7. All persons may be served alcohol.

When two or more rules conflict, the rule with the lowest number is used.

Obviously, it's very confusing to be a bartender in this town. Write a function called may-be-served? which will take in the following data (in this order): the person's age (as a number), the day of the week (as a literal), and the person's status in the militia (as a boolean; true means they are, false means that they are not).

It should return #t if the person may be served and #f if they may not be served. Example usage:

> (may-be-served? 14 'Thursday #f) ; should return false (under 18)
#f 
> (may-be-served? 20 'Sunday #t) ; should return false (no booze on Sunday)
#f
> (may-be-served? 20 'Friday #t) ; is ok to be served
#t

Problem 3
15.0 points

Oftentimes in mathematics it's useful to approximate an inexact number using a Taylor series. The Taylor series expansion for e(-x2) is:

Write a function called taylor-term which will take in two parameters: A value for X and an index. Your function should return the decimal value of the term in the Taylor series at that index. We'll start numbering the indices at zero, so the zeroth term in the above equation is 1, the first term is -X2, etc... Example usage:

> (taylor-term 3 3)
-121.5
> (taylor-term 2 3)
-10.66666666667

Problem 4
15.0 points

Now write a function called approximate which will take in two parameters: a value for X and the number of terms to use in the approximation. Your function should return an approximation for e(-x2) using the correct number of terms. For example:

> (approximate 1 1)
1
> (approximate 1 2)
0
> (approximate 2 2)
-3                                                                     

Problem 5
10.0 points

Write a function called my-remainder which will mimic the functionality of the built in function remainder. You should be able to generate your own sample data for this problem; just play with the built in function. Try with one or both numbers negative. Make sure your function returns an answer with the correct sign. Use tail recursion to solve this problem.

Problem 6
5.0 points

Write a function called my-expt which mimics the built in expt function. Note that your function only has to work for exponents which are positive integers. Once again, you can generate your own sample data using the built in expt function. Use augmenting recursion to solve this problem.

Problem 7
5.0 points

Using the substitution model of evaluation, show a stack trace of:

(my-expt 4 3)

Problem 8
10.0 points

Using only the functions define, cond, and else, write your own version of the if function. Call your function my-if. Example usage:

> (my-if #t 1 2)
1
> (my-if #f 2 3)
3
> (my-if (zero? 1) 3 4)
4
> (my-if (zero? 0) 5 6)
5

Problem 9
10.0 points

Write a function called number-of-evens which will take in a list of numbers and will return how many of those numbers are even. Example usage:

> (number-of-evens '(1 2 3 4 5 6 7 8 9 0))
5
> (number-of-evens '())
0 

Last Modified: May 27, 2001 by CS1321