jagomart
digital resources
picture1_Perimeter Ppt 80158 | Ch06 Item Download 2022-09-07 12-35-03


 248x       Filetype PPT       File size 0.09 MB       Source: www.cs.yale.edu


File: Perimeter Ppt 80158 | Ch06 Item Download 2022-09-07 12-35-03
the perimeter of a shape s1 s2 to compute the perimeter we need a function with s1 four equations 1 for each shape constructor the first three are easy perimeter ...

icon picture PPT Filetype Power Point PPT | Posted on 07 Sep 2022 | 3 years ago
Partial capture of text on file.
                            The Perimeter of a Shape
                      s1
                                s2
         To compute the perimeter we need a function with                     s1
            four equations (1 for each Shape constructor).
         The first three are easy …
             perimeter :: Shape -> Float                                              s2
             perimeter (Rectangle  s1 s2) = 2*(s1+s2)
             perimeter (RtTriangle s1 s2) =
                                   s1 + s2 + sqrt (s1^2+s2^2)
             perimeter (Polygon pts)      = 
                                   foldl (+) 0 (sides pts)
         This assumes that we can compute the lengths of the sides of a polygon.  This shouldn’t 
           be too difficult since we can compute the distance between two points with 
           distBetween.
           
                                                  
                Recursive Def’n of Sides
        sides       :: [Vertex] -> [Side]
        sides  []    = []
        sides (v:vs) = aux v vs
          where 
           aux v1 (v2:vs’) = distBetween v1 v2 : aux v2 vs’
           aux vn []       = distBetween vn v  : []
        -- aux vn []       = [distBetween vn v]
       But can we do better?  Can we remove the direct recursion, as a seasoned functional 
        programmer might?
       
                                
                 Visualize What’s Happening
                    B
           A                  C
              E            D
        The list of vertices is:  vs = [A,B,C,D,E]
        We need to compute the distances between the pairs of points (A,B), (B,C), 
          (C,D), (D,E), and (E,A).
        Can we compute these pairs as a list?
             [(A,B),(B,C),(C,D),(D,E),(E,A)]
        Yes, by “zipping” the two lists:
             [A,B,C,D,E] and [B,C,D,E,A]
          as follows:
            zip vs (tail vs ++ [head vs])
        
                                    
                  New Version of sides
       This leads to:
       sides   :: [Vertex] -> [Side]
       sides vs = zipWith distBetween vs 
                          (tail vs ++ [head vs])
       Where zipWith is a predefined function that is just like zip except that it applies 
         its first argument (a function) to each pair of values.  For example:
       zipWith (+) [1,2,3] [4,5,6]  [5,7,9]
       
                                
                                       Perimeter of an Ellipse
             There is one remaining case: the ellipse.  The perimeter of an ellipse is 
                 given by the summation of an infinite series.  For an ellipse with 
                 radii r and r :
                          1        2
                 p = 2r (1 - s)
                           1            i
             where s   = 1/4 e2
                        1
                                                    2
                       s    = s   (2i-1)(2i-3) e       for i >= 1
                       i       i-1
                                         4i2
                                    2      2
                    e   = sqrt (r     – r ) / r
                                   1      2      1
             Given s, it is easy to compute s                .
                        i                                 i+1
              
                                                                 
The words contained in this file might help you see if this file matches what you are looking for:

...The perimeter of a shape s to compute we need function with four equations for each constructor first three are easy float rectangle rttriangle sqrt polygon pts foldl sides this assumes that can lengths shouldn t be too difficult since distance between two points distbetween recursive def n v vs aux where vn but do better remove direct recursion as seasoned functional programmer might visualize what happening b c e d list vertices is distances pairs and these yes by zipping lists follows zip tail new version leads zipwith predefined just like except it applies its argument pair values example an ellipse there one remaining case given summation infinite series radii r p i...

no reviews yet
Please Login to review.