On Sun, Jan 31, 2010 at 5:22 PM, Steven D'Aprano
<ste...@remove.this.cybersource.com.au> wrote:
> On Sun, 31 Jan 2010 16:50:50 -0800, Chris Rebert wrote:
>>>>> How do you call a function of no arguments?
>>>>
>>>> It's not really a function in that case, it's just a named constant.
>>>> (Recall that functions don't/can't have side-effects.)
>>>
>>>
>>>>>> time.time(), random.random()
>>> (1264983502.7505889, 0.29974255140479633)
>>>>>> time.time(), random.random()
>>> (1264983505.9283719, 0.74207867411026329)
>>>
>>>
>>> They don't look terribly constant to me.
>>
>> Those aren't functions in the pure functional programming sense; which
>> is unsurprising since Python isn't a [pure] functional language. They
>> both involve side-effects. time() does I/O to the clock chip to see what
>> time it is, and random() uses and changes a global seed value variable
>> (which, in a double-whammy, takes its initial value from time()).
>
> Yes, but these tasks -- get the time, get a (pseudo) random number -- are
> not unique to Python. Surely even Lisp and Haskell code will sometimes
> need to know the time. Whether they are "pure functions" (functions in
> the mathematical sense) or impure, they're still functions in some sense.
> How do you deal with such impure functions?

Make the state explicit, i.e. monads or uniqueness typing.
In the case of random(), you have it take the seed explicitly as an
argument and have it return a new seed along with the "randomly"
generated number.
In the case of time(), you pass around an argument to represent the
state of the outside world. I don't quite grok monads, but
http://en.wikipedia.org/wiki/Uniqueness_type has a decent example of
the general uniqueness type approach.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to