How to use command line arguments in Haskell

As a quick note to self, this source code from the online version of Learn You a Haskell shows how to read command line arguments in Haskell:

-- from "Learn You a Haskell"

import System.Environment   
import Data.List  

main = do  
    args <- getArgs                  -- IO [String]
    progName <- getProgName          -- IO String
    putStrLn "The arguments are:"  
    mapM putStrLn args  
    putStrLn "The program name is:"  
    putStrLn progName

If you save that source code in a file named args.hs, and then compile it with ghc like this:


you can test it with no command line args like this:

$ ./args
The arguments are:
The program name is:

and then give it a few test command line args like this:

$ ./args foo bar
The arguments are:
The program name is:

As I show in the comments that I added, getArgs has the type IO [String], and progName has the type IO String.

I want to try writing a few simple scripts/programs in Haskell, so hopefully over time I’ll add more information on how to process command line arguments in Haskell. Until then, the Learn You a Haskell link I shared at the beginning has good information on handling I/O in Haskell.