Memoization - remembering past results
Memoization is the art of computer program optimization to speed up functions. Donald Michie coined the word memoization. Whenever, a memoized function is called for the first time, for a given input, its output value is calculated and cached. Next time when the same input is given as an argument, function does not compute the value but returns the value from cached location for that given input. In some programming language, we find some or other internal mechanism to implement memoization. But many programming languages require explicit work to implement memoization.
So, is it possible that a function can return different output for the same input? The answer is: this is possible. We can understand this from the following Scala example:
scala> val ab = scala.collection.mutable.ArrayBuffer(1,2,3,4) \\ Line One ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4) scala> print(ab.remove(1)) // Line...