SICP-2nd-Edition-Exercise-1.31-Haskell

From Boozled

Jump to: navigation, search

Contents

Exercise 1.31 From SICP in Haskell

Details from SICP are here

a. The (sum) procedure is only the simplest of a vast number of similar abstractions that can be captured as higher-order procedures.55 Write an analogous procedure called (product) that returns the product of the values of a function at points over a given range. Show how to define (factorial) in terms of (product). Also use (product) to compute approximations to π using the formula 52

\frac{\pi}{4} = \frac{2\cdot4\cdot4\cdot6\cdot6\cdot8\ldots}{3\cdot3\cdot5\cdot5\cdot7\cdot7\ldots}


b. If your (product) procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.

Attempt

Part a

Please read through the Scheme version of this exercise to see my train of thought

I found this exercise quite hard in Haskell. This was due to me struggling with trying to understand the way Haskell handles types etc. So far, for me, it appears that Haskells strong typing is a real pain to use. I cannot relate it to anything I have done in the past so it's taking some getting used to.

I'm hoping that I learn more Haskell this gets a lot easier. If it doesn't then I think I may need to try a language other than Haskell to do this in.

I also noticed that DrScheme computed the result a lot faster than Haskell and used a lot less RAM. For really big numbers Haskell would run out of stack space rapidly. If I increased it it took a long time to run. I think this could be attributed to my rubbish Haskell skills. I will take another attempt at this to remove the temporary variables etc. I think the way i have written this is recursive rather than iterative hence the problems.

Part b

Please look in the book at the recursive sum algorithm.

Haskell

  1. module Main where
  2. import Text.Printf
  3. --jw :: Integer -> Integer
  4. --accumulator :: (Integer -> Integer) -> Integer -> Integer -> (Integer -> Integer) -> (Integer -> Integer) -> Integer -> Integer
  5. jwp  :: Double -> Double
  6. jwn  :: Double -> Double
  7. accumulator :: (Double -> Double) -> Double -> Double -> (Double -> Double) -> (Double -> Double) -> Double -> Double
  8.  
  9. main =
  10.   do
  11.     let result = (johnwallis (2::Double) 200000) * 4.0
  12.     printf "\n"
  13.     printf "\tresult==%f\n" result
  14.     printf "\n"
  15.  
  16. johnwallis a b =
  17.   let
  18.     ap  = a + 2
  19.     inc  x = x
  20.     step y = y + 2
  21.   in
  22.     (jwstratp a b inc step 1 ) * (jwstratn ap b inc step 1 )
  23.  
  24. jwp n = n/(n + 1.0)
  25. jwn n = n/(n - 1.0)
  26.  
  27. jwstratp = accumulator jwp
  28. jwstratn = accumulator jwn
  29.  
  30.  
  31. accumulator op a b incr stepper acc =
  32.   let
  33.     p  = acc * (op (incr a))
  34.     a1 = stepper a
  35.   in
  36.     if a1 > b
  37.       then p
  38.       else accumulator op a1 b incr stepper p

Parent Course

6.001_Structure_and_Interpretation_of_Computer_Programs

Further Reading

Scheme version of this exercise

Personal tools