Reputation: 8448
In the shell you can string commands together, separated by a semicolon:
cd ../haskell; rm ./foo; ghc foo.hs; cd ../original_directory
It would be great if you could do a similar thing for command-line arguments for ghci, e.g.
ghci Foo.hs; a <- getFoo; print a
Is this possible?
Upvotes: 4
Views: 455
Reputation: 153172
You can use ghc -e
for this:
sorghum:~/programming% cat test.hs
getFoo = getLine
sorghum:~/programming% ghc test.hs -e 'do { a <- getFoo; print a }'
oenuth
"oenuth"
Upvotes: 7
Reputation: 120741
You can run ghci in silent mode and pass the instructions in through its standard input:
$ cat > ghciPipeTest.hs
getFoo = return 37 :: IO Int
$ ghci -v0 ghciPipeTest.hs <<< ' getFoo >>= print '
37
$
or
$ ghci -v0 ghciPipeTest.hs <<< $' a <- getFoo \n print a '
(assuming you use a bash-like shell. It also works with actual newlines inside the quotes)
Of course, it also works in non-silent mode, the output just looks a bit strange:
$ ghci ghciPipeTest.hs <<< $' a<-getFoo \n print a '
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( ghciPipeTest.hs, interpreted )
Ok, modules loaded: Main.
*Main> *Main> 37
*Main> Leaving GHCi.
Upvotes: 3