Implications of pure and constant functions
Posted Jun 11, 2008 1:40 UTC (Wed) by
rsidd (subscriber, #2582)
Parent article:
Implications of pure and constant functions
As MisterIO points out, the statement "pure functions cannot be
constant functions" is obviously wrong.
Another point: in a language like C, surely the pureness of a function depends on all the other functions in a program? For example, you say (accurately, as far as the definition of "pure" goes): "Because the global memory state remains untouched, two calls to the same pure function with the same parameters will have to return the same value." Your example is the strlen() function. But what if some other function has tampered with the contents addressed by your pointer in between the two calls, without modifying the pointer itself?
You hint at this issue later when you say of an example: "(The pure function is called just once) because there was no change to global memory known to the compiler between the two calls of the pure function." So the compiler needs to determine whether the memory addressed could have changed. This is not always possible, unless the compiler decides that any intervening non-pure function call could have changed the memory addressed -- a drastic assumption since in practice most such calls are probably harmless.
My take is, if you care about pure functions, use Haskell :)
(
Log in to post comments)