Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression evaluation mode in haskell for scripting

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.

like image 294
oliver Avatar asked Oct 25 '11 11:10

oliver


People also ask

How are expressions evaluated Haskell?

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.

What is Haskell evaluation?

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.

How do I run Haskell with ghci?

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.


1 Answers

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.

like image 140
FunctorSalad Avatar answered Oct 11 '22 05:10

FunctorSalad