[Haskell-beginners] Why getArgs is in the IO monad

Michael Orlitzky michael at orlitzky.com
Mon Apr 14 19:41:03 UTC 2014

On 04/14/2014 03:20 PM, Mike Meyer wrote:
>     From: "John M. Dlugosz" <ngnr63q02 at sneakemail.com
>     <mailto:ngnr63q02 at sneakemail.com>>
>     (And that begs the question of why getArgs needs to be monadic in
>     the first place.  It
>     doesn't change its value; it's a strict constant at run-time, and
>     not knowing it at
>     compile time is my problem how?)
> Actually, the value of the arguments can be changed - at least on some
> platforms. They are writable from C, if nothing else. What should
> getArgs do if some ffi changes the arguments before it's called?

You can do it right from within Haskell:

  Prelude> import System.Environment
  Prelude System.Environment> getArgs
  Prelude System.Environment> withArgs ["foo"] $ getArgs

It's useful if you have a command-line interface and the user mistypes
something; all you have to do is continue as if they passed "--help"
instead of a separate code path.

More information about the Beginners mailing list