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.
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:
Testing…
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.
You always add something, whether or not
ypasses or fails the test. This is wrong. Try using aniforcasestatement to differentiate the two different cases. Also, if you are using explicit recursion, then you must handle the empty list case explicitly: