thor
thor

Reputation: 22530

How to use GHCi :set args on quoted strings that include functions

I was trying to use GHCi and :set args for main, as follows:

:set args "(\x -> x )"

GHCi complains:

Couldn't read "\"(\\x -> x )\""as String

I don't understand why this can't be treated as a string. I could do without the quotes:

:set args (\x -> x )

But apparently, this is incorrect and it would be interpreted as 4 parameters instead of one, as shown by the following main function.

import System.Environment

main = do
  args <- getArgs
  putStrLn $ show $ length args

Any pointers on how to pass this into GHCi? (I could pass it to compiled ghc code from the command line.)

Thanks,

Upvotes: 2

Views: 180

Answers (1)

shachaf
shachaf

Reputation: 8930

Try this:

λ> :set args "(\\x -> x )"
λ> getArgs
["(\\x -> x )"]

Note that ghci argument parsing is not the same as shell argument parsing. If you use a string it'll be parsed as a Haskell string.

Another option is to use withArgs yourself:

λ> withArgs ["(\\x -> x )"] (getArgs >>= mapM_ putStrLn)
(\x -> x )

Upvotes: 6

Related Questions