Anonymous functions

Comprehension Questions

Math 250 - Mathematical Computing
Christopher Hanusa

6-1.  Use the command Function to define a function “firstLast” that takes as input a list and outputs the list consisting of the first element of the input and the last element of the input.

6-2. For each of the following anonymous functions, write a sentence describing what it does.  
     Find a suitable input (or inputs) and verify that the function does what you think it should.

(#^3+(#-1)^2)&

Range[Length[#]]&

#[[2]]&

6-3. Create an anonymous function that takes in an integer n and outputs the coordinate pair {x,y} where x is the n-th prime number and y is the (n+1)-st prime number.

6-4. Create an anonymous function that takes three inputs and outputs one list with all three inputs in reverse order.  (So for inputs 1, 4, 10, the output would be {10,4,1}.)

6-5. Before evaluating the following lines of code, anticipate what will be the result will be.  
     After evaluating the code, understand why the output is what it is and compare it with your expectations.  If there was an error, figure out what needs to be done to correct it.

Map[#[[2]]&,{{1,2},{3,4},{5,6},{7,8},{9,10}}]

Map[{#,#^2},Range[1,9,2]]

Map[Plot[x^#,{x,0,5}]&,{2,5,7}]

MapThread[#1^#2&,{Range[5],Range[5]}]

xlist={12,11,10,9}; 
ylist={1,2,3,4,5};
MapThread[(#1-#2)&,{xlist,ylist}]

alist=Range[0,Pi,Pi/6];
blist=Range[0,2Pi,Pi/3];
MapThread[Sin[#1]Cos[#2]&,alist,blist]

6-6. Use the function you created for Question 6-3 above to create a list of the coordinate pairs of {prime #n, prime #(n+1)} for n from 1 to 20.  Then display these coordinate pairs as a connected scatterplot.

6-7. Use Append to change the following coordinate pairs into coordinate triples by putting a zero into the third coordinate.

coords2D=Table[{i,i^2},{i,10}]