Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding `getArgs`

Tags:

haskell

I expect the following GetArgs.hs to print out the arguments passed into it.

import System.Environment

main = do
    args <- getArgs
    print args

But, after loading it in ghci, I get the following error:

ghci> main 3 4 3

<interactive>:39:1:
    Couldn't match expected type `a0 -> a1 -> a2 -> t0'
                with actual type `IO ()'
    The function `main' is applied to three arguments,
    but its type `IO ()' has none
    In the expression: main 3 4 3
    In an equation for `it': it = main 3 4 3

Since print has this type:

ghci> :t print print :: Show a => a -> IO ()

I would've expected print args to have worked.

Why not?

like image 613
Kevin Meredith Avatar asked Jul 03 '14 00:07

Kevin Meredith


1 Answers

print args works fine. What doesn't work is main 3 4 3. main doesn't take any argument, but you're trying to call it with three.

If you want to simulate calling your program with command line parameters from ghci, you can use the :main command (with a colon in front). Alternatively you can compile your program and actually run it from the command lines with the given arguments.

like image 126
sepp2k Avatar answered Sep 18 '22 15:09

sepp2k