'Cant match "Int -> Bool" with actual type "(Int,Bool)"

I'm writing a simple code to add numbers in the list with the correct conditions into a new list. But I'm getting an error that one of the input is (Int, Bool) instead of Int, how did this happen? Here's the code I'm writing

module FilterList where

-- Definisi dan Spesifikasi
countIf :: [Int] -> (Int,Bool) -> [Int]
{-Mencari elemen yang memenuhi kriteria tertentu-}
isPos :: Int -> Bool
{-True jika positif-}
isNeg :: Int -> Bool
{-True jika negatif-}
isKabisat :: Int -> Bool
{-True jika berada pada tahun kabisat (dapat dibagi 4 atau 400 tetapi tidak bisa dibagi 100)-}
isEmpty :: [Int] -> Bool
{-True jika list kosong-}

-- Realisasi
isEmpty li = (length li) == 0
isPos a = a >= 0
isNeg a = a < 0
isKabisat a = (mod a 4) == 0 && ((mod a 100) /= 0 || (mod a 400) == 0) 
countIf li func
    | isEmpty li = []
    | func (head li) = [] ++ [head li] ++ countIf (tail li) func
    | otherwise = [] ++ countIf (tail li) func

Some stuff are in Indonesian but only in the comments so the general idea is still there.



Solution 1:[1]

The signature is wrong: the func is a predicate, so:

countIf :: [Int] -> (Int -> Bool) -> [Int]

Your isEmpty function is however equivalent to null :: Foldable f => f a -> Bool (which is more efficient), and countIf is a special version of a "flipped" version of filter :: (a -> Bool) -> [a] -> [a]. The name countIf also suggests that you are counting the number of matched elements, not a list of items that match the predicate.

Using [] ++ xs makes not much sense: this will simply return the xs, and [x] ++ ys is equivalent to x : ys. You use pattern matching to avoid using head and tail, so:

countIf :: [a] -> (a -> Bool) -> [a]
countIf [] func = []
countIf (h:t) func
    | func h = h : countIf t func
    | otherwise = countIf t func

but countIf can thus be implemented as:

countIf :: [a] -> (a -> Bool) -> [a]
countIf = flip filter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem