Ad
  • Custom User Avatar

    It's lazier; it works when is is infinite. I suspect (though I don't exactly recall) that's why I wrote it that way in the first place. Of course, the requirement to return the lexicographically least solution prevents doing anything useful with infinite lists, so it got transformed into this solution.

    If you're bothered by the possibility of creating nested loops, you could manually fuse it:

    indices is = unfoldr go (0, is)
      where go (_, []) = Nothing
            go (a, _:bs) = Just (a, (a+1,bs))
    

    But I don't think that is necessary.

    The real problem here is the use of (!!), turning what should be a quadratic algorithm cubic.

  • Default User Avatar

    well spotted. thanks. Fixed.