83

I've been told you can interpret Haskell files (which I assume means they will work like Ruby/Python/Perl). I can't find the command line option on GHC to do this, though. It always wants to compile my file. Took a look at GHCi as well, but it always dumps me into a repl.

I'm basically wanting to just do ghc -i MyFile.hs (where -i is a made up flag that I'm pretending correllates to interpreted mode) and have it execute so that I can get quick feedback while I'm trying out ideas and learning.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
  • 1
    There's a lot to like about ghci: it makes it very easy to test small parts of your program. I personally am just in the habit of using ghci to test things. You can still run the top-level `main` action by typing `main` (or using `:main` if you need to pass it command-line arguments). – Daniel Wagner Dec 30 '11 at 16:41

4 Answers4

138
$ runhaskell MyFile.hs

Alternatively, runghc (they're the same thing). ghci MyFile.hs will also start an interactive REPL session with MyFile.hs loaded, but if you want to run a main program then runhaskell is the way to go.

It's probably a good idea to get into the habit of testing parts of your program as isolated units in GHCi rather than running the whole thing each time, but obviously for shorter scripts it's simplest and easiest just to run the entire thing.

ehird
  • 40,602
  • 3
  • 180
  • 182
  • 4
    For whatever reason (haskell-platform on OSX), `runhaskell` is not on my path, but `runghc` was. (so +1 for mentioning `runghc`) – derekv May 04 '15 at 19:10
  • 1
    I know this thread has long been closed but is the runhaskell interpreter jit compiled? – nbroeking Sep 23 '15 at 01:47
41

You can have a script like this:

#!/usr/bin/env runhaskell
main = putStrLn "hello world"

After making the file executable (ie chmod +x haskell_script), you can run it like any other shell script.

David Miani
  • 14,518
  • 2
  • 47
  • 66
  • 4
    Not *quite* like any other shell script. The return value of the last expression executed, if not `()`, is automatically output by `runhaskell`, just as it is in GHCi. That doesn't happen with an actual executable compiled normally with `ghc`. – Mark Reed Nov 28 '12 at 23:21
  • @MarkReed Could you give an example? – Linus Arver Jan 23 '15 at 22:00
  • I might be able to come up with an artificial one, but I'd be hard-pressed to do so. It's not really a practical concern, as you will want most scripts to do some sort of IO as their final action anyway. I just thought it was worth noting. – Mark Reed Jan 24 '15 at 02:21
12

Open the GHC interpreter by running ghci in a terminal, and then load a file typing :load example.hs. More details in this link.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

To run the code written in a file, say myfile.txt, containing simple lines of code which work in the GHC interpreter, like:

let a = 0 in a:[1,2]
let x = [1,2] in x ++ [3,4]

you can do:

ghc -e ':script myfile.txt'

Edit

On Windows, double quotes are required:

ghc -e ":script myfile.txt"

Instead, one can also open GHCi and do :script myfile.txt.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225