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.
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.
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:
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
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
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
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.
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.
Using the substitution model of evaluation, show a stack trace of:
(my-expt 4 3)
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
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