I want to use list comprehension to print only true
listTo::(a->b)->[a]->[b]
listTo f list=[f(x)|x<-list,x==True]
isTrue::Int->Bool
isTrue n
|n>=5 =True
|n<5 =False
listTo isTrue[1,2,3,4,5,6,7]
But this doesn't work. Why is it?
[f(x)|x<-list,x==True]
This says "give me a list of f x
1, where x
comes from list
and x
is equal to True
.
But for your case, you've passed list
as a list of numbers. And it looks like you want to only include those numbers for which f x
returns True
. So rather than comparing x
against True
and emitting f x
, you should be doing the opposite. Something more like:
[x | x <- list, f x == True]
But we can improve this a little further. ==
is a function which takes two arguments and returns True
if they're equal and False
if they're not. So f x == True
is going to return True
if f x
is True
, and False
if f x
is False
. Seems like a waste of effort; you could just write f x
!
[x | x <- list, f x]
So that's how you use a list comprehension to do this. But as postman noted, there is already a standard library function to do exactly this: filter
. That's what you would use if you were writing code to get things done, rather than to learn how things work.
1 Note that f(x)
is just parsed as f
applied to the parenthesised sub-expression (x)
. Putting parentheses around x
does nothing, so that's exactly equivalent to f x
. Haskell's syntax for function application is not C-like function(arg1, arg2, arg3)
, but rather function arg1 arg2
, where the arguments are simply written out after the function (separated by whitespace if necessary). Parentheses are used only for controlling precedence and forming tuples. If you start trying to call functions on multiple arguments by putting parentheses around the argument list you're going to run into even more errors.
You should really consider using filter
, like this:
filter isTrue [1,2,3,4,5,6,7]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With