I’ve always found it awkward to have a function or expression that requires use of the values, as well as indices, of a list (or array, applies just the same) in Haskell.
I wrote validQueens below while experimenting with the N-queens problem here …
validQueens x =
and [abs (x!!i - x!!j) /= j-i | i<-[0..length x - 2], j<-[i+1..length x - 1]]
I didn’t care for the use of indexing, all the plus and minuses, etc. It feels sloppy. I came up with the following:
enumerate x = zip [0..length x - 1] x
validQueens' :: [Int] -> Bool
validQueens' x = and [abs (snd j - snd i) /= fst j - fst i | i<-l, j<-l, fst j > fst i]
where l = enumerate x
being inspired by Python’s enumerate (not that borrowing imperative concepts is necessarily a great idea). Seems better in concept, but snd and fst all over the place kinda sucks. It’s also, at least at first glance, costlier both in time and space. I’m not sure whether or not I like it any better.
So in short, I am not really satisfied with either
- Iterating thru by index bounded by lengths, or even worse, off-by-ones and twos
- Index-element tuples
Has anyone found a pattern they find more elegant than either of the above? If not, is there any compelling reason one of the above methods is superior?
Borrowing
enumerateis fine and encouraged. However, it can be made a bit lazier by refusing to calculate the length of its argument:(In fact, it’s common to just use
zip [0..]without naming itenumerate.) It’s not clear to me why you think your second example should be costlier in either time or space. Remember: indexing is O(n), where n is the index. Your complaint about the unwieldiness offstandsndis justified, and can be remedied with pattern-matching:Now, you might be a bit concerned about the efficiency of this double loop, since the clause
(j, y) <- lis going to be running down the entire spine ofl, when really we just want it to start where we left off with(i, x) <- l. So, let’s write a function that implements that idea:Having made this function, your function is not too hard to adapt. Pulling out the predicate into its own function, we can use
allinstead ofand:Or, if you prefer point-free notation: