Replacing functions with Table Lookups

后端 未结 3 756
慢半拍i
慢半拍i 2021-02-08 04:01

I\'ve been watching this MSDN video with Brian Beckman and I\'d like to better understand something he says:

Every imperitive programmer goes through this

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 04:21

    In the context of functional programming, there is the concept of referential transparency. A function that is referentially transparent can be replaced with its value for any given argument (or set of arguments), without changing the behaviour of the program.

    Referential Transparency

    For example, consider a function F that takes 1 argument, n. F is referentially transparent, so F(n) can be replaced with the value of F evaluated at n. It makes no difference to the program.

    In C#, this would look like:

    public class Square
    {
        public static int apply(int n)
        {
            return n * n;
        }
    
        public static void Main()
        {
            //Should print 4
            Console.WriteLine(Square.apply(2));
        }
    }
    

    (I'm not very familiar with C#, coming from a Java background, so you'll have to forgive me if this example isn't quite syntactically correct).

    It's obvious here that the function apply cannot have any other value than 4 when called with an argument of 2, since it's just returning the square of its argument. The value of the function only depends on its argument, n; in other words, referential transparency.

    I ask you, then, what the difference is between Console.WriteLine(Square.apply(2)) and Console.WriteLine(4). The answer is, there's no difference at all, for all intents are purposes. We could go through the entire program, replacing all instances of Square.apply(n) with the value returned by Square.apply(n), and the results would be the exact same.

    So what did Brian Beckman mean with his statement about replacing function calls with a table lookup? He was referring to this property of referentially transparent functions. If Square.apply(2) can be replaced with 4 with no impact on program behaviour, then why not just cache the values when the first call is made, and put it in a table indexed by the arguments to the function. A lookup table for values of Square.apply(n) would look somewhat like this:

                  n: 0 1 2 3 4  5  ...
    Square.apply(n): 0 1 4 9 16 25 ...
    

    And for any call to Square.apply(n), instead of calling the function, we can simply find the cached value for n in the table, and replace the function call with this value. It's fairly obvious that this will most likely bring about a large speed increase in the program.

提交回复
热议问题