Question

In: Advanced Math

Haskell Map and Filter 6. Let f1 = filter (\ x -> x > 0) and...

Haskell

Map and Filter

6. Let f1 = filter (\ x -> x > 0) and f2 = filter (\x -> x < 10), and let nbrFilter g x = length (filter g x).

a. Rewrite f1(f2[-5..15]) so that it uses function composition to apply just one function to the list.

b. Rewrite the nbrFilter function definition to have the form

nbrFilter g = function composition involving length and filter … and leaving out x.

Solutions

Expert Solution

Given Functions:

f1(x): This filter will pass the values which are more than 0,

f2(x): This filter will pass the values which are less than 10, and

nbrFilter g = function composition involving length and filter both.

So nbrFilter g(x) = length(filter(x)), where filter can be f1 or f2 or any other.

Part - a)

So to apply this function composition : f1(f2[-5..15]), we can make a function(say f3) having both conditions:

f3(x) = filter(\x -> x > 0 and \x -> x < 10).

Let's do the python implementation:

for f1(x), we have:

def f1(x):
z = []
for y in x:
if(y>0):
z.append(y)
return z

and for f2(x), we have:

def f2(x):
z = []
for y in x:
if(y<10):
z.append(y)
return z

and finally below is the python implementation of the composition function(f3):

def f1(x):
z = []
for y in x:
if(y<10 and y>0):
z.append(y)
return z

Part - b)

Since it is given: nbrFilter g(x) = length(filter(x)), where filter can be f1 or f2 or any other.

lets assume, we are using f3 filter here.

So, nbrFilter g(x) = length(filter(\x -> x > 0 and \x -> x < 10))

Below is the python implementation of nbrFilter g(x):

def f1(x):
z = []
for y in x:
if(y<10 and y>0):
z.append(y)
return len(z)


Related Solutions

Let X be a metric space and t: X to X be a map that preserves...
Let X be a metric space and t: X to X be a map that preserves distances: d(t(x), t(y)) = d(x, y). Give an example in whicht is not bijective. Could let t: x to x+1,x non-negative, but how does this mean t is not surjective? Any help will be much appreciated!
Let X~BIN(40,0.1). Let Y=max(0,x-3). FindE[Y].
Let X~BIN(40,0.1). Let Y=max(0,x-3). FindE[Y].
Let X be an exponential distribution with mean=1, i.e. f(x)=e^-x for 0<X< ∞, and 0 elsewhere....
Let X be an exponential distribution with mean=1, i.e. f(x)=e^-x for 0<X< ∞, and 0 elsewhere. Find the density function and cdf of a) X^1/2 b)X=e^x c)X=1/X Which of the random variables-X, X^1/2, e^x, 1/X does not have a finite mean?
Let the joint p.d.f f(x,y) = 1 for 0 <= x <= 2, 0 <= y...
Let the joint p.d.f f(x,y) = 1 for 0 <= x <= 2, 0 <= y <= 1, 2*y <= x. (And 0 otherwise) Let the random variable W = X + Y. Without knowing the p.d.f of W, what interval of w values holds at least 60% of the probability?
Let X and Y be random variables. Suppose P(X = 0, Y = 0) = .1,...
Let X and Y be random variables. Suppose P(X = 0, Y = 0) = .1, P(X = 1, Y = 0) = .3, P(X = 2, Y = 0) = .2 P(X = 0, Y = 1) = .2, P(X = 1, Y = 1) = .2, P(X = 2, Y = 1) = 0. a. Determine E(X) and E(Y ). b. Find Cov(X, Y ) c. Find Cov(2X + 3Y, Y ).
1 Let f(x, y) = 4xy, 0 < x < 1, 0 < y < 1,...
1 Let f(x, y) = 4xy, 0 < x < 1, 0 < y < 1, zero elsewhere, be the joint probability density function(pdf) of X and Y . Find P(0 < X < 1/2 , 1/4 < Y < 1) , P(X = Y ), and P(X < Y ). Notice that P(X = Y ) would be the volume under the surface f(x, y) = 4xy and above the line segment 0 < x = y < 1...
. Let x, y ∈ R \ {0}. Prove that if x < x^(−1) < y...
. Let x, y ∈ R \ {0}. Prove that if x < x^(−1) < y < y^(−1) then x < −1.
Let f(x) = a(e-2x – e-6x), for x ≥ 0, and f(x)=0 elsewhere. a) Find a...
Let f(x) = a(e-2x – e-6x), for x ≥ 0, and f(x)=0 elsewhere. a) Find a so that f(x) is a probability density function b)What is P(X<=1)
let R be a region bounded by x = 0 and x =1 and y =...
let R be a region bounded by x = 0 and x =1 and y = 0 and y = 1. Suppose the density is given by 1/y+1.Notice that R is denser near the x axis. As a result we might expect the centre of mass to be below the geometric center(1/2,1/2). Also since the density does not depend on x we do expect moment of inertia about the x axis to be 1/2. verify the moment of inertia about...
Let h( x) = ( x − 5)( x − 6)( x − 7) a. Explain...
Let h( x) = ( x − 5)( x − 6)( x − 7) a. Explain why Rolle’s Theorem can be applied on the interval [-5,7]. b. Find all values of c in the open interval (5,7) such that f'(c)=0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT