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.
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.
The order of evaluation in Haskell is determined by one and only one thing: Data Dependence. An expression will only be evaluated when it is needed, and will not be evaluated if it is not needed.
Haskell uses a special form of evaluation called lazy evaluation. In lazy evaluation, no code is evaluated until it's needed. In the case of longList , none of the values in the list were needed for computation. Lazy evaluation has advantages and disadvantages. It's easy to see some of the advantages.
If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.
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
and ghc-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With