Home Ask Login Register

Developers Planet

Your answer is one click away!

Frank D. February 2016

Printing two arguments

I am trying to :load file "hello.hs" and keep getting the errors below.

I am using WinGHCi 1.0.6.

I have printed just one argument and it worked but when I try to print two arguments I get a Not in scope error as shown below.

My hello.hs code contains the code below.


module Main where
import System.Environment
main :: IO ( )
main = do 
        args <- getArgs  --Getting first arg
        putStrLn("Hello," ++args!!0)
    do  args1 <- getArgs putStrLn("Hello," ++args1!!1)  --Getting second arg

----------Errors while trying to do :load command --------

hello.hs:6:37: Not in scope: ‘args’

    The last statement in a 'do' block must be an expression
      args1 <- getArgs putStrLn ("Hello," ++ args1 !! 1)

    Not in scope: ‘args1’
    Perhaps you meant ‘args’ (line 5)


Daniel Wagner February 2016

Put the putStrLn command on a separate line. The second do is not necessary (in fact, it is necessary not to have it). Additionally, you should know that the compiler views hard tabs as padding out to the next eight-space boundary; so although things look correctly indented here on SO, your code's mixture of tabs and spaces is incorrect. I recommend either using a single tab at the beginning of each line in the do block or converting all tabs to spaces. See also my diatribe on tab styles. So:

main = do 
        args <- getArgs  --Getting first arg
        putStrLn("Hello," ++args!!0)
        args1 <- getArgs
        putStrLn("Hello," ++args1!!1)  --Getting second arg

This is the smallest change needed to make things work. Additionally, since I see no reason to believe the arguments will change between the two calls to getArgs, you may as well omit the second call and simply reuse args:

main = do 
        args <- getArgs
        putStrLn("Hello," ++args!!0)
        putStrLn("Hello," ++args!!1)

Post Status

Asked in February 2016
Viewed 2,038 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life