正如在其他地方多次指出的那样(例如1,2,...) Haskell 中的脚本非常强大。
一种快速方法也可以是 ghc 表达式评估模式。这就是我实际上发现自己越来越多地使用的东西(我真的很喜欢 ruby 中的这个功能)。
一个小示例任务:
“找出所有包含 HEAD 和特定修订版之间 git diff 的文件夹”
git diff --stat 9e2b68 | ghc -e \
"getContents >>= return.(Data.List.nub).map(fst.break('/'==).head.words).lines"
这看起来有点笨拙,可能是因为我真的不知道使用 ghc -e 的详细信息。
鉴于所有有趣的部分只是 nub.map(fst.break('/'==).head.words).lines
实际的表达式似乎有点啰嗦。
- 如何告诉 ghc 我需要使用的模块,这样我就不需要使用全名来限定它们?
- 我可以让 ghc 获取某种包含我经常使用的模块的配置文件吗?
我真的很高兴看到其他用例中的一些示例,这些示例将帮助我改进使用 haskell 处理此类小脚本的方式!
旁注:命令行 foo 向导可能会嘲笑这一点,但我觉得使用 haskell 比 bash 脚本更舒服,所以这就是我想要使用的。
As noted multiple times elsewhere (eg. 1,2,...) scripting in haskell can be quite powerful.
A quick way can also be the ghc expression evaluation mode. this is what I actually find myself using more and more (I really like this feature in ruby).
A little example task:
"Find out all the folders that contained git diffs between the HEAD and a specific revision"
git diff --stat 9e2b68 | ghc -e \
"getContents >>= return.(Data.List.nub).map(fst.break('/'==).head.words).lines"
This looks a little clunky, probably because I don't really know the details of using ghc -e
.
Given that all the interesting part is just the nub.map(fst.break('/'==).head.words).lines
the actual expression seems a little wordy.
- How do I tell ghc about modules I need to use so I don't need to qualify them using the full name?
- Can I make ghc pick up some kind of a configuration file that contains modules I frequently use?
I'd really appreciate seeing some examples from other usecases that will help my improve the way I use haskell for those kinds of little scripts!
Sidenote: Commandline-foo wizards will probably laugh at this but I feel much more comfortable using haskell then bash scripting so this is what I want to use.
发布评论
评论(3)
关于模块:
ghc -e
使用您的~/.ghci
文件,因此在这种情况下,您需要添加:m +Data.List
(从 GHC 7 左右开始也支持import Data.List(nub)
)。关于包:您可以使用 ghc-pkg hide somepackage 和 ghc-pkg expose somepackage 来定义默认的可见包集(尽管默认情况下包是公开的;也许我误解了你的问题)。
您可能会发现 eddie 很有用。
Regarding modules:
ghc -e
uses your~/.ghci
file, so in this case, you'd add:m +Data.List
to it (import Data.List(nub)
is also supported since GHC 7 or so).Regarding packages: You can use
ghc-pkg hide somepackage
andghc-pkg expose somepackage
to define the default set of visible packages (packages are exposed by default though; maybe I misunderstand your question).You might find eddie useful.
除了每个 .ghci 文件的标准配置(除了全局 ~/.ghci 之外,您还可以在某些目录中拥有专门的 .ghci 文件,然后在此之前读取这些文件),您还可以将 Haskell 源文件传递为命令行参数,
ghc -e "expression" path/to/Source.hs
。然后,表达式
将在模块源
的上下文中进行计算。In addition to the standard configuration per the .ghci file (apart from your global ~/.ghci, you can have specialised .ghci files in some directories, which will then be read before that), you can also pass a Haskell source file as command line argument,
ghc -e "expression" path/to/Source.hs
.expression
will then be evaluated in the context ofmodule Source
.就像 FunctorSalad 所说,
ghc -e
加载~/.ghci
,这样您就可以使用它来添加您可能在脚本中使用的任何样板文件。在您的情况下,您可以添加到
~/.ghci
文件,然后运行:Like FunctorSalad said,
ghc -e
loads the~/.ghci
so you can use it to add any boilerplate that you might use in your script. In your case you could addto you
~/.ghci
file and then run: