Consider the following Haskell-definition:
nfib 0 = 1
nfib 1 = 1
nfib (n + 2) = nfib n + nfib (n + 1) + 1
main = print (nfib 30)
Execution of the (via hbc) compiled file
results in 2692537 in 255 seconds.
Changing the above definition like this:
nfib 0 = 1
nfib 1 = 1
nfib n = nfib (n - 2) + nfib (n - 1) + 1
main = print (nfib 30)
results in 2692537 in 142 seconds.
I think, that is not a desirable implementation
of pattern-matching. To speed up my programs, therefore,
I have to avoid (n+k)-patterns. In Miranda the definition
using (n+2) is faster (166 seconds in comparison with
173 seconds).
But I do not want to program without using the
well-known advantages of such patterns:
- increased readability
- distinct left-hand-sides