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 6917197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:58+00:00 2026-05-27T09:41:58+00:00

I have been tasked to write a function which is basically same as findIndices

  • 0

I have been tasked to write a function which is basically same as findIndices but in a recursive way. So far I have managed to make this:

getIndicesFor :: (a -> Bool) -> [a] -> [Int]
getIndicesFor x (y:ys) = (fst (head(filter ((x y).snd) as ))):getIndicesFor x ys where
    as = (zip [0..] (y:ys))

But this results in an error saying: “Could’t match expected type b0 -> Bool' with actual typeBool'”. And I can’t seem to figure out the problem.

Thanks for your answers.

  • 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-05-27T09:41:59+00:00Added an answer on May 27, 2026 at 9:41 am

    This function can be written using do notation. I don’t feel bad presenting this because you probably won’t get credit for submitting this as an answer, because it does not use explicit recursion:

    import Control.Monad (guard)
    
    getIndicesFor :: (a -> Bool) -> [a] -> [Int]
    getIndicesFor f xs = do
      (n, x) <- zip [0..] xs
      guard (f x)
      return n
    
    -- in fact you could write it compactly as a list comprehension
    -- though I personally avoid list comprehensions, as I find "do" syntax clearer
    -- getIndicesFor f xs = [n | (n, x) <- zip [0..] xs, f x]
    

    Testing…

    ghci> take 5 $ getIndicesFor even [0 ..]
    [0, 2, 4, 6, 8]
    ghci> take 5 $ getIndicesFor even [2, 4 ..]
    [0,1,2,3,4]
    

    So the concept is simple, right? pick an element from the list, test it with the specified function, and add its index to the list if it passes.

    If you must write this function using explicit recursion, then you will need to handle the two cases differently: does this particular element pass or fail the test? If it passes, then you add its index to the list of results. If it fails, then you don’t. This is the main problem with what I see in your code right now.

    getIndicesFor x (y:ys) = blahblah : getIndicesFor x ys
    

    You always add something, whether or not y passes or fails the test. This is wrong. Try using an if or case statement to differentiate the two different cases. Also, if you are using explicit recursion, then you must handle the empty list case explicitly:

    getIndicesFor x [] = ???
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked to write a device driver for an embedded device which
I have been tasked with converting several php classes into java classes, which is
I have been tasked with improving the performance of a slow running process which
I have been tasked to write a module for importing data into a client's
I have been tasked to write a script that will enable tcp and named
I have been tasked to write a script to change a specific value in
I have been tasked with updating a old project that I did not write.
I have been tasked with creating pg_dump files for a db on which there
I have been tasked with implementing a PKI library in C# for a company
I have been tasked with going through a number of ColdFusion sites that have

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.