Alex Rivera | Logout

Expression evaluation mode in haskell for scripting

Asked 2011-10-25T11:34:02.627
15

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.

Edit
Report

1 Answer

5

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 add

let script f = getContents >>= return f
import Data.List

to you ~/.ghci file and then run:

git diff ... | ghc -e "script.nub.map(fst.break(=='/').head.words).lines"
answered 2011-10-25T12:37:29.013

Your Answer