Q:

Performing operation on a Sublist that passes a certain test

I am trying to write a function that, when giving two numbers and a list that they belong to, would calculate everything between those numbers (in value).
For example:
averageBetween 4 8 [1,2,8,2,7,2,4,6] = average [4,6,7,8] = 6,25
What I've managed to do so far is:
average::[Int]->Double
average [] = 0
average xs = (fromIntegral (sum xs)) / (fromIntegral (length xs)
averageBetween::Int->Int->[Int]->Double
averageBetween a b xs

And now I'm kinda stuck, I can't really figure out a way to "filter" my list. Any help would be appreciated.
~                         

A:

The good news is that, aside from a missing closing parenthesis, your average function works as expected and can be used in your averageBetween function. To find the average of values between a given a and b in a list, we'll need to filter the list, as you said, eliminating values greater than max a b and less than min a b. This is important in case we are given our arguments in reverse. Our predicate would thus look something like this:
(\x -> (x >= (min a b)) && (x <= (max a b)))

We can then call our average function on the filtered list, yielding our final function definitions:
average :: [Int] -> Double
average [] = 0
average xs = (fromIntegral (sum xs)) / (fromIntegral (length xs))

averageBetween :: Int -> Int -> [Int] -> Double
averageBetween a b xs = average $ filter (\x -> (x >= (min a b)) && (x <= (max a b))) xs

Which can be used just like you'd expect:
ghci>> averageBetween 4 8 [1,2,8,2,7,2,4,6]
6.25

