title: How to check to see if a function in R is a constant function?
body: <p>An R function is passed to me, which is defined on some interval of the Real Line and returns a numeric value.
  Is there a way to check to see if the function is constant?</p>
  
  <p>Example functions:</p>
  
  <pre><code>f1&lt;-function(x) {11}
  f2&lt;-function(x) {x+2}
  f3&lt;-function(x) {1+1}
  f4&lt;-function(x) {return(3)}
  </code></pre>
  
  <p>I am looking for a test that will say that f1,f3,f4 are constant functions but f2 isn't. Any Ideas? </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Frank's and Gregor's (<strong>edit:</strong> and Michael Lawrence's second solution)solutions below all work for all the 4 test cases given above (Marat's and Michael's don't work on all 4 cases). So there is already solutions. But extra bonus points if you can find a solution which also gives the right answer for the following 3 test functions:</p>
  
  <pre><code>f5 &lt;- function(x) ifelse(x == 5.46512616432116, 0, 1)
  f6 &lt;- function(x) ifelse(x == 5.46512616432116, 0, 0)
  f7 &lt;- function(x) {x - x}
  </code></pre>