How to get the Index of an element in a list, by not using "list comprehensions"?

The problem with your attempt is simply that when you say:

let ((n,m):ns) = zip [0..(length (x:xs))] (x:xs)

then n will always be 0. That's because you are matching (n,m) against the first element of zip [0..(length (x:xs))] (x:xs), which will necessarily always be (0,x).

That's not a problem in itself - but it does mean you have to handle the recursive step properly. The way you have it now, positions _ _, if non-empty, will always have 0 as its first element, because the only way you allow it to find a match is if it's at the head of the list, resulting in an index of 0. That means that your result will always be a list of the correct length, but with all elements 0 - as you're seeing.

The problem isn't with your recursion scheme though, it's to do with the fact that you're not modifying the result to account for the fact that you don't always want 0 added to the front of the result list. Since each recursive call just adds 1 to the index you want to find, all you need to do is map the increment function (+1) over the recursive result:

positions' :: Eq a => a -> [a] -> [Int]
positions' _ [] = []
positions' a (x:xs) =
    let ((0,m):ns) = zip [0..(length (x:xs))] (x:xs)
    in if (a == m) then 0:(map (+1) (positions' a xs))
       else (map (+1) (positions' a xs))

(Note that I've changed your let to be explicit that n will always be 0 - I prefer to be explicit this way but this in itself doesn't change the output.) Since m is always bound to x and ns isn't used at all, we can elide the let, inlining the definition of m:

positions' :: Eq a => a -> [a] -> [Int]
positions' _ [] = []
positions' a (x:xs) =
    if a == x
    then 0 : map (+1) (positions' a xs)
    else     map (+1) (positions' a xs)

You could go on to factor out the repeated map (+1) (positions' a xs) if you wanted to.

Incidentally, you didn't need explicit recursion to avoid a list comprehension here. For one, list comprehensions are basically a replacement for uses of map and filter. I was going to write this out explicitly, but I see @WillemVanOnsem has given this as an answer so I will simply refer you to his answer.

Another way, although perhaps not acceptable if you were asked to implement this yourself, would be to just use the built-in elemIndices function, which does exactly what you are trying to implement here.


We can make use of a filter :: (a -> Bool) -> [a] -> [a] and map :: (a -> b) -> [a] -> [b] approach, like:

positions  :: Eq a => a -> [a] -> [Int]
positions x = map fst . filter ((x ==) . snd) . zip [0..]

We thus first construct tuples of the form (i, yi), next we filter such that we only retain these tuples for which x == yi, and finally we fetch the first item of these tuples.

For example:

Prelude> positions 'o' "foobaraboof" 
[1,2,8,9]

Your

    let ((n,m):ns) = zip [0..(length (x:xs))] (x:xs)

is equivalent to

== {- by laziness -}
    let ((n,m):ns) = zip [0..] (x:xs)
== {- by definition of zip -}
    let ((n,m):ns) = (0,x) : zip [1..] xs
== {- by pattern matching -}
    let {(n,m)     = (0,x)
        ;      ns  =         zip [1..] xs }
== {- by pattern matching -}
    let { n        =  0
        ;   m      =    x
        ;      ns  =         zip [1..] xs }

but you never reference ns! So we don't need its binding at all:

positions' a (x:xs) =
    let { n = 0  ;  m = x } in
    if (a == m) then n : (positions' a xs)
                else     (positions' a xs) 

and so, by substitution, you actually have

positions' :: Eq a => a -> [a] -> [Int]
positions' _ [] = []
positions' a (x:xs) =
    if (a == x) then 0 : (positions' a xs)      -- NB: 0 
                else     (positions' a xs) 

And this is why all you ever produce are 0s. But you want to produce the correct index: 0, 1, 2, 3, ....

First, let's tweak your code a little bit further into

positions' :: Eq a => a -> [a] -> [Int]
positions' a  =  go xs
  where
  go  []  = []
  go (x:xs) | a == x    =  0 : go xs            -- NB: 0 
            | otherwise =      go xs 

This is known as a worker/wrapper transform. go is a worker, positions' is a wrapper. There's no need to pass a around from call to call, it doesn't change, and we have access to it anyway. It is in the enclosing scope with respect to the inner function, go. We've also used guards instead of the more verbose and less visually apparent if ... then ... else.

Now we just need to use something -- the correct index value -- instead of 0.

To use it, we must have it first. What is it? It starts as 0, then it is incremented on each step along the input list.

When do we make a step along the input list? At the recursive call:

positions' :: Eq a => a -> [a] -> [Int]
positions' a  =  go xs 0
  where
  go  []    _ = []
  go (x:xs) i | a == x    =  0 : go xs (i+1)    -- NB: 0 
              | otherwise =      go xs (i+1)

_ as a pattern means we don't care about the argument's value -- it's there but we're not going to use it.

Now all that's left for us to do is to use that i in place of that 0.