negative :: [Int] -> Int negative list = [ x | x <- list, if list<0 then x=1 else x=0]
Makes no sense for several reasons:
- the result type is an
Int, so by using list comprehension as outer expression, you are generating a list;
xis an element of the list
x <- list,
if list < 0 then … else …makes no sense since
listis a list, and can not be compared with a single element; and
- variables can not be assigned a new value. Variables are defined once, here by
x <- list. You can make a more locally scoped variable with the same name, but that is not a good idea here.
negative :: [Int] -> Int negative list = length (filter (< 0) list)
we can make these point-free, and let this work with any
negative :: (Num a, Ord a) => [a] -> Int negative = length . filter (< 0)
CLICK HERE to find out more related problems solutions.