Are two functions equal?

前端 未结 5 1156
轮回少年
轮回少年 2020-12-06 11:08

[Edit]

The general question seems incredibly hard to solve. Here is a significantly restricted version of this question.


How do I dete

5条回答
  •  渐次进展
    2020-12-06 11:23

    Assuming you're talking about a family of JavaScript functions that are constrained to one integer argument, and which do not have visible external side-effects, I still think it's going to be generally not possible to determine equality. Consider this:

    function a(n) {
      var today = new Date();
      if (today.getFullYear() === 2012 && today.getMonth() === 1 && today.getDate() === 3)
        return 0;
      return n * n;
    }
    

    That function looks a whole lot like

    function b(n) { return n * n; }
    

    Except on 3 Feb 2012, when it will look exactly like:

    function c(n) { return 0; }
    

    If you're really talking about a practical piece of real software to perform such an analysis, and if you really don't have much control over the details of the functions to be tested, it doesn't seem possible. Perhaps you can construct a set of "rules" that the functions must follow, but even at that the prospect of determining equality without testing every value in the domain seems pretty remote.

    edit — I just thought of something: what if your two functions return functions? Now, to determine whether f == g, you first have to figure out whether the functions returned by f(n) for all n are equal to the functions returned by g(n) for all n. Hmm ...

提交回复
热议问题