tuples_summing_to_zero that
takes a list of two-tuples as input and returns those tuples from the
input list whose values sum to zero. The tuples in the output list should
appear in the same order as they did in the input list. For full credit,
use a list comprehension in your solution.
> tuples_summing_to_zero [] [] > tuples_summing_to_zero [(1,0), (0,-1)] [] > tuples_summing_to_zero [(-1,-2), (0,0), (2,-1), (-2,2)] [(0,0),(-2,2)] > :type tuples_summing_to_zero tuples_summing_to_zero :: Num a => [(a,a)] -> [(a,a)]
average_dist that takes a list of Cartesian
coordinates (two-tuples) and computes the average distance from the
origin to each point in the input list that lies in the first quadrant.
(Points whose X and Y coordinates are both positive are in the first
quadrant.) You may assume that at least one such point is in the input
list. For full credit, use a list comprehension in your solution and avoid
redundant computation by using let or where.
> average_dist [(1,1)] 1.41421 > average_dist [(-2,1),(1,1),(2,-3),(0,0)] 1.41421 > average_dist [(2,2), (-2,2), (3,3)] 3.53553 >:type average_dist average_dist :: (Ord a, Floating a) => [(a,a)] -> a
area_trap
that uses the trapezoid rule to approximate the area. (i.e. "connect
the dots")area_rect
that approximates the area with a rectangle whose height is equal
to the first Y-coordinate.
integrate with a type at least as general as:
(Num a, Ord b, Num b) => (b -> c -> c -> a) -> b -> b -> b -> (b -> c) -> a.
(Whew!) The first input to integrate is an approximation function like
the ones from the previous problem. (That's the "(b -> c -> c -> a)"
component of the type signature.) The next three arguments are the
spacing between evaluation points and the starting and ending X-coordinates, respectively.
(The "b -> b -> b" in the type.) The last argument is the function to
be integrated. (The "(b -> c)" in the type.)
> integrate area_trap 0.1 0 100 (\x->x*x) 333333.500000013 > integrate area_rect 0.1 0 100 (\x->x*x) 332833.500000013 > integrate area_trap 0.3 0 1 (\x->x) 0.5 > :type integrate integrate :: (Num a, Ord b, Num b) => (b -> c -> c -> a) -> b -> b -> b -> (b -> c) -> a
trap_int that performs numerical integration
using the trapezoid method with a spacing of 0.1 between evaluation points.
It takes three inputs: the starting and ending X-coordinates and a function
to be integrated. For full credit, define trap_int using partial
application of integrate.
> trap_int 0 100 (\x->x*x) 333333.500000013 > trap_int 0 10 (\x->x) 50.0 > :type trap_int trap_int :: Double -> Double -> (Double -> Double) -> Double
most_frequent that returns the item that occurs most frequently in an input list. (In the case of a tie, it doesn't matter which you return.) Try to use built-in list operations as much as possible to reduce the amount of code you have to write.
> most_frequent [] Program error: Not defined on empty lists > most_frequent [1,2] 1 > most_frequent ['b','a','c','a','d'] 'a'