4

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?

4

2 回答 2

7

您可以ghc -e为此使用:

sorghum:~/programming% cat test.hs
getFoo = getLine
sorghum:~/programming% ghc test.hs -e 'do { a <- getFoo; print a }'
oenuth
"oenuth"
于 2012-08-24T15:21:49.020 回答
3

您可以在静默模式下运行 ghci 并通过其标准输入传递指令:

$ cat > ghciPipeTest.hs
getFoo = return 37 :: IO Int
$ ghci -v0 ghciPipeTest.hs <<< ' getFoo >>= print '
37
$

或者

$ ghci -v0 ghciPipeTest.hs <<< $' a <- getFoo \n print a '

(假设您使用类似 bash 的外壳。它也适用于引号内的实际换行符)

当然,它也可以在非静音模式下工作,只是输出看起来有点奇怪:

$ ghci ghciPipeTest.hs <<< $' a<-getFoo \n print a '
GHCi,版本 7.4.1: http://www.haskell.org/ghc/ :? 寻求帮助
加载包 ghc-prim ... 链接 ... 完成。
加载包 integer-gmp ... 链接 ... 完成。
正在加载包库...链接...完成。
[1 of 1] 编译 Main(ghciPipeTest.hs,解释)
好的,加载的模块:Main。
*Main> *Main> 37
*Main> 离开 GHCi。

于 2012-08-24T14:40:03.550 回答