user3645265
user3645265

Reputation: 773

Get args Haskell

I'm having problems with an exercise, and can not understand the error. It should be a simple exercise with args:

import System.IO
import System.Environment

main= do
args < - getArgs
nomeficheiro <- return( args !! 0)
putStrnLn ( "Name is" ++ nomeficheiro)

Then i should run it, with : $ ./comando James

The error:

 <interactive>:51:1:
parse error on input ‘$’
Perhaps you intended to use TemplateHaskell

I've read other doubts about args at this fórum and I didn't find any answer that could help me

Upvotes: 0

Views: 2207

Answers (1)

Zeta
Zeta

Reputation: 105886

$ ./comando James isn't meant to be run on GHCi. Instead, $ at the start of the line indicates that the following line should be run in your bash/cmd/shell, not in GHCi:

# in your favourite shell, in the correct directory
./comando James

If you want to run main with arguments within GHCi, you can use :main args:

ghci> :main James

Further remarks

Your current code isn't indented correctly, so make sure that you fix this too. Also, you can use let nomeficheiro = head args instead of … <- return …. Keep in mind that this could lead to problems if one doesn't supply any argument to your program, since head [] calls error.

Upvotes: 6

Related Questions