Suppose I have two F # functions:
let sq x = x*x let tm = DateTime.Now
Obviously, sq is clean because it will always return the same value for a given input, while tm is unclean because it will return a different value each time it is called.
In general, is there a way to determine if a particular function in F # is clean or unclean without analyzing what it does, in other words, reading it line by line?
Alternatively, is there a way to annotate a function to tell the compiler that the function is clean or unclean when you write it?
Finally, when calling a function that is part of a common language runtime (such as DateTime), how can you determine if it is clean or unclean without trying to execute it?
Note: by βcleanβ I mean the definition from Wikipedia: http://en.wikipedia.org/wiki/Pure_function ( permalink )
In computer programming, a function can be described as pure if both of these statements about the function being performed are:
The function always evaluates the same value of the result, given the same argument of the value). The value of the result of the function cannot depend on hidden information or the state, which can change as the program continues to run or between different program executions, and cannot depend on any external input from input / output devices.
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to input / output devices.
Jonnyboats
source share