I am wondering how Haskell pattern matching is resolved internally and how this impacts performance. Say we have a function that is expensive to compute so we precompute the first and / or more frequently used values and pattern match on the corresponding input before making the actual computation itself:
expensiveComputation :: Int -> Int expensiveComputation 1 = 101 expensiveComputation 2 = 3333333 expensiveComputation 3 = 42 ... expensiveComputation n = the actual function We could precompute a lot of these cases, thousands if we wanted, but do we? I'm assuming that it takes time for Haskell to actually find the pattern that matches the input, so maybe it is actually quicker to compute the, say, 1000th value instead of making a 1000 pattern matches?
arrayRangewhich will memoize a certain range of inputs into a flat array with constant time lookup, which is basically what you are doing manually here.