07.01.2014 Views

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> <strong>Glorious</strong> <strong>Glasgow</strong> <strong>Haskell</strong><br />

<strong>Compilation</strong> <strong>System</strong> User’s <strong>Guide</strong>,<br />

Version 6.10.4 19 / 224<br />

2.4.3.3 <strong>The</strong> :main and :run commands<br />

When a program is compiled and executed, it can use the getArgs function to access the command-line arguments. However,<br />

we cannot simply pass the arguments to the main function while we are testing in ghci, as the main function doesn’t take its<br />

directly.<br />

Instead, we can use the :main command. This runs whatever main is in scope, with any arguments being treated the same as<br />

command-line arguments, e.g.:<br />

Prelude> let main = <strong>System</strong>.Environment.getArgs >>= print<br />

Prelude> :main foo bar<br />

["foo","bar"]<br />

We can also quote arguments which contains characters like spaces, and they are treated like <strong>Haskell</strong> strings, or we can just use<br />

<strong>Haskell</strong> list syntax:<br />

Prelude> :main foo "bar baz"<br />

["foo","bar baz"]<br />

Prelude> :main ["foo", "bar baz"]<br />

["foo","bar baz"]<br />

Finally, other functions can be called, either with the -main-is flag or the :run command:<br />

Prelude> let foo = putStrLn "foo" >> <strong>System</strong>.Environment.getArgs >>= print<br />

Prelude> let bar = putStrLn "bar" >> <strong>System</strong>.Environment.getArgs >>= print<br />

Prelude> :set -main-is foo<br />

Prelude> :main foo "bar baz"<br />

foo<br />

["foo","bar baz"]<br />

Prelude> :run bar ["foo", "bar baz"]<br />

bar<br />

["foo","bar baz"]<br />

2.4.4 <strong>The</strong> it variable<br />

Whenever an expression (or a non-binding statement, to be precise) is typed at the prompt, GHCi implicitly binds its value to the<br />

variable it. For example:<br />

Prelude> 1+2<br />

3<br />

Prelude> it * 2<br />

6<br />

What actually happens is that GHCi typechecks the expression, and if it doesn’t have an IO type, then it transforms it as follows:<br />

an expression e turns into<br />

let it = e;<br />

print it<br />

which is then run as an IO-action.<br />

Hence, the original expression must have a type which is an instance of the Show class, or GHCi will complain:<br />

Prelude> id<br />

:1:0:<br />

No instance for (Show (a -> a))<br />

arising from use of ‘print’ at :1:0-1<br />

Possible fix: add an instance declaration for (Show (a -> a))<br />

In the expression: print it<br />

In a ’do’ expression: print it

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!