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

  • Home
  • SEARCH
  • 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 8020597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:44:02+00:00 2026-06-04T21:44:02+00:00

Studying Haskell, i’m trying to code a function takeIf that given a condition and

  • 0

Studying Haskell, i’m trying to code a function takeIf that given a condition and a list returns all the list’s element that satisfies the condition.

Examples of desired output:

takeIf (>0) [] --> []
takeIf (>0) [-1, 0, 1, 2] --> [1,2]

I tried this definition:

takeIf cond [] = []
takeIf cond (x:xs) = if (cond x) 
                    then x:(takeIf cond xs)
                    else []:(takeIf cond xs)

but it doesn’t work.

My first question is: I have

:t takeIf --> ([a] -> Bool) -> [[a]] -> [[a]]

why? Why not:

:t takeIf --> (a -> Bool) -> [a] -> [a]

How can I get this code to work?

This is the error I get:

enter image description here

If helpful i’m using ghci

  • 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-04T21:44:03+00:00Added an answer on June 4, 2026 at 9:44 pm
    []:(takeIf cond xs)
    

    Here you’re trying to prepend [] as the new first element to the result of takeIf cond xs. Since [] is a list, GHC infers from that that the result of takeIf cond xs must be a list of lists. Since the result of takeIf has the same type as its argument, that means xs must also be a list of lists. And since x is an element of xs, x must consequently be a list.

    It seems like you intended []: to mean “prepend nothing to the list”, but [] isn’t nothing, it’s the empty list. [] : [], doesn’t give you [], it gives you [[]]. Likewise [] : [1,2,3] would give you [[], 1, 2, 3] – except that that’s not well-typed, so what it really gives you is a type error.

    If you want to prepend nothing to a list, just don’t prepend anything to the list, i.e. just use someList instead of [] : someList.

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

Sidebar

Related Questions

I am studying Haskell. I have the next question: The List type is a
I'm studying this code, and there is a + in the color function which
Studying compilers course, I am left wondering why use registers at all. It is
After studying TCP/UDP difference all week, I just can't decide which to use. I
I started studying Haskell one week ago and have one strange problem. I created
While studying the Collection API, we find that some methods ( add , remove
When studying a snippet of unknown Python code, I occasionally bump into the varName.methodName()
I was studying an open source code where I came across the following line
I am studying an existing Perl program, which includes the following line of code:
i studying TDateTime functions and procedure, but not found something that allow me to

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.