Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!brolga!uqcspe!batserver.cs.uq.oz.au!brendan From: brendan@batserver.cs.uq.oz.au (Brendan Mahony) Newsgroups: comp.society.futures Subject: Re: C's sins of commission (was: (pssst...fortran?)) Message-ID: <5049@uqcspe.cs.uq.oz.au> Date: 1 Oct 90 00:03:08 GMT References: <9009202236.AA21344@raven.pa.dec.com> <63647@lanl.gov> <5006@uqcspe.cs.uq.oz.au> Sender: news@uqcspe.cs.uq.oz.au Reply-To: brendan@batserver.cs.uq.oz.au Lines: 70 My line: -> Even allowing functions to look at global state variables leads to confusion, -> letting them change them means you don't have a function and terms are -> not terms. What is the point of such confusion? peter@ficc.ferranti.com (Peter da Silva) writes: >OK, how do you implement the function "rnd", which returns a random >number, without letting it have side effects? Fine use a tuple valued function function rnd (oldseed : integer) -> (newseed, ran : integer) begin newseed := ... ran := ... end The function would be used in code (seed, ran) := rnd(seed); But seriously folks what you really want is a procedure. Note that this function (which makes explicit the action of the of the operation) cannot (easily) be used as an integer term, and must be accompanied with an update to seed for the whole thing to work properly. rnd is not an integer term, its purpose is not solely to define the value of an integer. Why then do you want to include rnd in the grammar of integer terms? Is it just to save a few keystrokes in the initial coding? Pretty silly given that this is such a small part of the software cycle. The idea is not conciseness but clarity! Side effects in rnd may not worry you, but that is only because everyone knows that it must have side effects. If it was not as well known it would be easy to overlook the fact that a function called rnd, appearing deep in some complicated expression, actually goes and plays with global variables. I think it is very worthwhile to have a clear seperate notion of term: expression defining a value and not to have to worry about any side effects when reading the code, and also when using the function. The second point is important, for my function an expression like 4*second(rnd(seed)) + 3-second(rnd(seed)) has a well defined meaning. For a side effect rnd it may not be as clear what the result is. At the very least order of evaluation becomes important. I know we don't care with rnd but we often will. Note also that the compiler also knows for sure that it will only have to evaluate rnd(seed) once for this expression, if side-effects are possible this optimisation becomes very difficult to determine. For instance it is not even clear that two references to the same variable will yield the same result. Concider seed*rnd + seed There are several possible evaluation strategies for this expression, all yield different results. Are the few keystrokes saved worth the extra complication? Can't think of any more problems at the moment, but I am sure they are there. -- Brendan Mahony | brendan@batserver.cs.uq.oz Department of Computer Science | heretic: someone who disgrees with you University of Queensland | about something neither of you knows Australia | anything about.