Homework # 2:  Iterators, Lists, Functions, and Recursion

Below are a few simple exercises that will help you understand some of the problems you may have encountered while working on Module 2.  Be sure you understand the solutions to these exercises because these problems will come up again later in this course.

You may use the Documentation Center to assist you with these problems, but you should not copy your answers verbatim from the Documentation Center.  After you understand the solution in the Documentation Center, you should present the solution in your own words.  Try to avoid jargon and special notation;  use standard English and classical Mathematical notation.  Also you should not copy examples from the Documentation Center;  rather you should provide your own examples to ensure that you understand the concepts.

Please format your solutions appropriately.  Use text format, not input format, when you are typing text.  Write coherent sentences and paragraphs;  part of your grade will depend on how clearly you present your ideas.

You must work on these exercises by yourself, not in pairs;  if you get stuck and cannot find the solution in the Documentation Center, you may consult the labbie or the instructor during their office hours.

Iteration

Iteration is one of the key programming techniques for solving repetitive problems.  Throughout this course, when solving problems using Mathematica, you will make extensive use of iteration.

Problems

1.  What is an iterator?

2.  Give two examples of built in functions in Mathematica that use an iterator.  In each case, explain how the iterator works.

Lists

Lists are one of the key constructs in Mathematica.  Here are some additional problems that may help clarify how lists work in Mathematica.

Problems

1.  Suppose that you have created a list called myList consisting of 10 elements.  What is the Mathematica notation for the fifth element in myList?  Give an example to show that your notation is correct.

2.  What is the main function used in Mathematica to create lists?

3.  Use the Mathematica function you cited in Problem 2 to create a list of the cubes of the first n numbers for different values of n.

4.  Write a Mathematica function that takes as input a list of complex numbers and returns as output a new list consisting of twice the complex conjugate of each of the complex numbers in the original list.

5.  Suppose you have a list called myList consisting of any n numbers.  What do you think Mathematica would do if you type in (myList^3)?  Try it.  Do you think this notation generalizes to other functions?  What happens if you type Log[myList]?

Function Definition

Defining new functions is one of the key tasks in programming.  Here are some problems that will help you to correctly define new functions.

Problems

1.  State the three laws of function definition in Mathematica.

2.  Explain what would happen if you were to violate the first law.  Give examples.

3.  Explain what would happen if you were to violate the second law.  Give examples.

4.  Explain what would happen if you were to violate the third law.  Give examples.

Recursion

Recursion is a powerful technique for constructing functions in Mathematica.  Here are some additional problems that will help clarify how recursion works.

Problems

1.  The binomial coefficients C[n,k] satisfy the following recurrence:

C[n,k] = 0   if  k<0 or k>n
	C[n,0] = 1
	C[n,k] = C[n-1,k] + C[n-1,k-1]

Write a recursive Mathematica function to compute C[n,k] for any two integers k, n.
(Note:  There is a built in function in Mathematica to compute the binomial coefficients, but for this problem you may not use this built in function.)

2.  Test that the function you defined in Problem 1 is correct by comparing various values of your function C[n,k] to the built in function in Mathematica for the binomial coefficients.  Are there any values of n and k for which your function C[n,k] differs from the built in function in Mathematica for the binomial coefficients?

3.  Using the function you defined in Problem 1, define another function that for any fixed n generates the list consisting of the n+1 numbers C[n,k], k=0,…,n.

4.  Calculate the sum of the elements in the list in Problem 3 for different values of n.  What do you notice about this sum?