I have a Haskell homework exercise to manually re-create findIndices using recursion.
I've simplified the problem by hard-coding a cons 0 each time it finds an index that meets the criteria:
positions :: (a -> Bool) -> [a] ->[Integer]
positions p [] = []
positions p (x:xs)
| p x = 0 : positions p xs
| otherwise = positions p xs
But instead of outputting a 0 in each position of the output list, I want it to output the index but I cannot work out how to do this using a count variable.
I've tried to simplify this by just outputting the index to every item but can't work this out either so I've got:
pos :: [a] -> [Integer]
pos [] = []
pos (x:xs) = 0 : pos xs
which returns a 0 for every item in the list so [2,3,4,5] returns [0,0,0,0] as expected but I can't work out how to make it return [0,1,2,3]
I can make it count how many items are in the list but I can't work out how to put the count variable into the list.
pos1 :: [a] -> Integer
pos1 [] = 0
pos1 (x:xs) = let count = pos1 xs in count+1
Any help in "Haskell for Dummies" language would be much appreciated.