Chapter 4 Discussion VI

The alternating series.

The alternating series is

[1, -1/2, +1/3, -1/4, +1/5, -1/6, ...]
  1. Define altTerm n to be the nth term in the sequence.

  2. Define altSeq m to be the list of the first m terms in the sequence.

  3. Write a function to find the sum of the first k terms of the alternating series.

  4. Write waltSum, which takes in a list of numbers, multiplies each term by the corresponding term in the alternating series, and then adds up the sums.

    Example:

     waltSum [5,10,15] == 1*5 + (-1/2)*10 + (1/3)*15
    
  5. The goalt x function adds up the terms in the alternating series until it comes on a term that is less than x in absolute value.

    Example:

     goalt 0.21 == 0.5833...
    

    Explanation of example:

    • The sequence starts with 1. Since this term is greater than x=0.21, it is included in the sum. Cumulative sum = $1$.
    • The next term in the sequence is $-1/2$. Since the absolute value of this term is greater than 0.21, it is also included in the sum. Cumulative sum = $1 - 1/2 = 0.5$.
    • The next term of the sequence is $1/3$. Since 1/3 is greater than 0.21, it is included. Cumulative sum = $1-1/2 + 1/3 \approx 0.8333$.
    • The next term is $-1/4$. Its absolute value is 0.25, which is still greater than 0.21. Cumulative sum = $1-1/2+1/3-1/4 \approx 0.5833$.
    • The next term is $1/5$. Since $0.20$ is less than $0.21$, the process stops, giving a final answer of about $0.5833$.

Remember: fromIntegral when you need to divide something by an Int.

Other dangers: Haskell distinguishes between exponentiation for integers (^) and floating point numbers (**).