3 ms·
I doubt pure functions exist in any language. For instance, consider the C function 'unsigned twice(unsigned a) { return a + a; }'. Here are some ways it can
by jepler 9y ago
I doubt pure functions exist in any language. For instance, consider the C function 'unsigned twice(unsigned a) { return a + a; }'. Here are some ways it can be non-pure:
It could be called with an argument that has never been given a value:
int j; twice(j);
It could be called with an argument that is volatile:
volatile int j; twice(j); twice(j);
it could be called at just the right moment to exhaust the allocated stack:
int j=4; twice(j); char arr[8388608]; twice(j);
[I didn't actually find where the C(99) standard states that an implementation may have a maximum stack size! I am clearly using the wrong search terms]
A signal could arrive during the execution of twice() and cause it never to return
int j=4; twice(j);
- robinpokorny 9y agoAt least true functional languages have them, think Haskell. I do not know much (read: any) C, so I have a question: why does an argument that has never been given a value make it impure?
- jepler 9y agoUsing an undefined value leads to undefined behavior under the C spec. This "undefined behavior" could be a visible side effect (in the helpful case, a diagnostic on stderr like when you're using valgrind on your program; in the unhelpful case literal demons flying out of your nose) and it need not be the same on each use of the undefined value either.
- jepler 9y ago.. some haskell implementations apparently had (practically) limited stack size, at least as of 2007, so as long as "pure" functions still allocate stack frames, it's possible for the result of the pure function call to be "stack exhausted" in one case and not another. https://mail.haskell.org/pipermail/haskell-cafe/2007-November/034017.html https://mail.haskell.org/pipermail/haskell-cafe/2007-Novembe...