Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using condition in list comprehension in haskell

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?

like image 316
Alston Avatar asked Dec 15 '22 11:12

Alston


2 Answers

[f(x)|x<-list,x==True]

This says "give me a list of f x1, 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.

like image 81
Ben Avatar answered Dec 30 '22 04:12

Ben


You should really consider using filter, like this:

filter isTrue [1,2,3,4,5,6,7]
like image 28
postman Avatar answered Dec 30 '22 05:12

postman