Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:41:52+00:00 2026-06-16T05:41:52+00:00

I am trying to write a function that takes in a predicate and a

  • 0

I am trying to write a function that takes in a predicate and a list and returns a list that satisfies the predicate.

So, for instance, I want something like this:

haskell> count_if (x > 3) [2,3,4,5,6] 
[4,5,6]

Here’s what I have so far:

count_if f [] = 0 
count_if f (x:xs) 
  | f x = x : count_if f xs
  | otherwise = count_if f xs 

My question is, how do I test this function using a predicate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:41:54+00:00Added an answer on June 16, 2026 at 5:41 am

    Your countif function is struggling to count anything because you told it to make a list:

    count_if f [] = 0  -- fine, makes sense
    count_if f (x:xs) 
      | f x = x : count_if f xs  -- Oops, no!
      | otherwise = count_if f xs  -- yup
    

    Notice that 1:[2,3] = [1,2,3], so : is for putting an extra element on the front of a list. If you want to count, you want a number, not a list. (Putting x on the front sounds a lot like filter, which gives you all the elements where your predicate is true, but you wanted to count, which is different.)

    You’ll spot this type of error more easily if you tell the compiler what you were expecting by giving an explicit type signature like count_if :: (a -> Bool) -> [a] -> Int. Instead of putting x on the front with x:, let’s add one with 1+, giving

    count_if :: (a -> Bool) -> [a] -> Int
    count_if f [] = 0 
    count_if f (x:xs) 
      | f x = 1 + count_if f xs  -- adds one to the total from the rest
      | otherwise = count_if f xs  
    

    Now that can be tested like this:

    > count_if (>5) [1..10]
    5
    > count_if (=='e') "Time is of the essence"
    5
    > count_if even [1..100]
    50
    

    Now you can make count_if using filter. The type of filter is filter :: (a -> Bool) -> [a] -> [a] and it gives just the elements that you need:

    > filter (>5) [1..10]
    [6,7,8,9,10]
    > filter (=='e') "Time is of the essence"
    "eeeee"
    

    but then do length on the result:

    countif' :: (a -> Bool) -> [a] -> Int
    countif' f xs = length (filter f xs)
    

    But that can be written slightly neater as

    countif :: (a -> Bool) -> [a] -> Int
    countif f = length . filter f 
    

    because . is function composition – this says filter with f, then take the length.

    (Pointfree geeks would prefer to write this as countif = (length.).filter but that’s a lesson for another day!)

    Using standard functions like filter and length can result in performance enhancements you might not spot by yourself. If you test countif (>0) [1..1000000] against count_if (>0) [1..1000000], you’ll find it runs noticably faster. It’s a good idea to get to know prelude functions like filter, foldr, scanr etc from the prelude because of this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Haskell function that takes makes use of this function:
I am trying to write haskell function that takes list of lists and find
I'm trying to write a merge-sort function that takes a list and a comparison
I'm trying to write a function that takes two string arguments and returns the
I am trying to write a function that takes some strings and does something
I'm trying to write a Java function that takes a Class<?> and returns a
I trying to complete this exercise; Write a Lisp function that takes as input
I am trying to write a list function that takes a simple list and
I'm trying to write a haskell function that takes in two lists of integers
I'm trying to write a Powershell function that takes an array argument. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.