Homework # 3b:  Lists and Variable Names

Below are a few simple exercises that will help you understand some of the problems you may have encountered while working on Module 3b.  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.

Lists

Typically here is more than one way to skin a cat.  Below is an exercise to illustrate that there are often many ways in Mathematica to code the solution to a problem.

Problem

Give three ways in Mathematica to find the sum of the elements in a list of numbers.  In each case give examples to verify that your program is correct.

Variable Names

Typically variables are used to store numbers.  But functions, lists, strings, and many other types of objects can also be represented by variables in Mathematica.

Just like variables that represent numbers, variables that represent functions, lists, and strings can be used as arguments to functions.  When this happens, we use f_ not f[x_] as the variable for a function;  we use L_ not {L_} as the variable for a list;  and we use s_ not "s_" as the variable for a string.  When we want to replace  a variable by an actual function or list or string, we us the name of the function or the list or the string -- for example, we would use Sin not Sin[x] to replace the argument representing a function.

The definition (the right hand side of the := sign) will determine whether your variable stands for a function, a list, or a string.  Using { } or " " around a variable will only confuse Mathematica.  For example, if you write {L_}, then Mathematica will think that L is not a list!  Rather Mathematica will think that your list is {L}.  Thus, {1,2,3} is a list, but 1,2,3 is not a list!  The stuff inside the brackets is not a list!  The list is the thing that already has brackets.

Problems

1.  You are told to write a program oddList that takes as input a non-empty list, and returns as output a list consistng of only the odd numbered elements in the original list -- that is, the function returns a new list consisting of the first, third, fifth,... elements of the original list.  Which of the following syntaxes could you use on the left hands side to define the function oddList?
	a.  oddList[{alist_}] 
	b.  oddList[{alist}_] 
	c.  oddList[{alist_}_]
	d.  oddList[alist_]
	e.  oddList[{}_]
	f.   oddList[{ _}]

2.  Write the program oddList described in Problem 1.  Give examples to show that your program operates correctly on any non-empty list.

3.  You are told to write a program applyF that takes as input a list of real numbers and a function whose domain is the real numbers, and returns as output a list of real numbers consisting of the function evaluated at the even numbered elements in the original list -- that is, the function evaluated at the second, fourth, sixth,... elements of the original list.  Which of the following syntaxes could you use on the left hands side to define the function applyF?
	a.  applyF[{alist_},f[x]_]
	b.  applyF[{alist_},f[x_]] 
	c.  applyF[{alist_},f[x_]_] 
	d.  applyF[{alist}_,f[x_]_] 
	e.  applyF[{alist_}_,f[x_]_] 
	f.  applyF[{alist}_,f[x]_]
	g.  applyF[alist_,f_]
	h.  applyF[{}_,f_]
	i.  applyF[{}_,f_[x_]]
	j.  applyF[{ _},f[x_]]

4.  Write the program applyF described in Problem 3.  Give examples to show that your program operates correctly on any list of real numbers and any function whose domain is the real numbers.