Lennart Augustsson writes:
>
> > I know you could go wild with monads, c. classes etc. and try to
> > squirrel away these things, but argv is a constant (in a sane world),
> > so why not treat it as such (ditto for the environment)? Thoughts?
> Well, in 1983 Thomas Johnsson and I decided that this was the
> way to go and this is the way it is in LML since those days.
>
> It certainly has its advantages, but I must say that I'm leaning
> in the Haskell direction now. Having the arguments and the environment
> (and why not standard input, and maybe all the files in the file system?)
> as global constants means that your program has dependencies on the
> outside world that are not clearly visible in the program. It goes
> against the grain of pure functional programming.
>
But 1.3 already does this, stdin,stdout&stderr are global handle
constants - so why not do it for something that *really* is constant?
Clearly, you could do very silly stuff like
isSilly = any (=="-silly") System.argv
dum :: Int -> Int
dum n =
if isSilly then
factorial n
else
nfib n
and radically change the meaning of a pure function depending on the
command-line arguments you fed the program, but I don't see how
plumbing the options around in a monad, makes the above more functional.
Shooting yourself in the foot might be harder with Haskell, but
certainly not impossible :-)
I'd actually prefer if stdin&stdout were not constants, but argv was ;)
Having operators just like getArgs for the standard handles
getStdin :: IO Handle
getStdout :: IO Handle
getStderr :: IO Handle
would not prevent an implementation from providing useful redirection
operations like the following
stdRedirect :: (Handle,Handle,Handle) -> IO a -> IO a
where the second argument is performed using a different standard
handle triple. This sort of operation is Really Useful if you want to
reuse existing stdio-based code in a GUI, say, and in general go
beyond the ancient&austere stdio in your implementation (I've been
wanting to do a souped-up Haskell version of Phong Vo&Korn's sfio for
a long time).
--sigbjorn