What you are trying to do is one of the more common ways of using list comprehension.
List comprehension is described in Haskell98 report as having this
aexp -> [ exp | qual1 , ... , qualn ] (list comprehension, n>=1)
qual -> pat <- exp (generator)
| let decls (local declaration)
| exp (guard)
what is means is that you provide between [ and | what you are trying to have as a new list element. This is where you use functions to change your list elements such as [ head a | will make a list of heads of some a
After | come qualifiers.
They can be
- generators (like
x <- xs )
- local declarations
- or guards
What you need in your case are 2 guards (x > a and x < b) and one generator (x <- xs)
so what you need is
[ x | x <- xs , x > a, x < b ]
Some argue that list comprehension is for begginers and should later be substituted with higher order functions like filter and map so in your case you would acutally only need filter
some examples
inRange' a b xs = filter (\ x -> (x > a) && (x < b) ) xs
-- eta reduced version (without using xs)
inRange'' a b = filter (\ x -> (x > a) && (x < b) )
if … then … else …. I'm currently not really capable of providing a well written answer, so here's the short one:[x | x <- xs, x > a, x < b]. Feel free to use it in an answer.