I’m working my way through Graham Hutton’s Haskell book, and in his recursion chapter, he often pattern-matches on “n+1”, as in:
myReplicate1 0 _ = []
myReplicate1 (n+1) x = x : myReplicate1 n x
Why that and not the following, which (1) seems functionally identical and (2) more intuitive in terms of understanding what’s happening with the recursion:
myReplicate2 0 _ = []
myReplicate2 n x = x : myReplicate2 (n-1) x
Is there something I’m missing here? Or is it just a matter of style?
Those are n+k patterns (that should be avoided!) in the first function. Both functions do the same thing, except for the n+k one not matching negative numbers. The latter one, however, is recommended, and can be adopted if you want no negative numbers on purpose, because the n+k patterns are sheduled to be removed anyways.
So no, you’re missing nothing, and it is indeed a matter of style, but I rarely see n+k patterns in the wild.