Quick Haskell Trick
The Haskell REPL (Read Print Eval Loop) - GHC interpreter, ghci
invoked with:
$ ghci
or:
$ stack repl
or:
$ cabal new-repl
or:
$ cabal repl
is an environment enabling fast iteration.
You can test a particular function and that’s cool and all, but you can also run an entire program in the interpreter. You do that with:
ghci> :main
or
ghci> main
The flow is as follows:
- You make some changes in your editor
- You
:r
or:reload
- You make some more changes to make it compile
- You
:r
again - You run
:main
to play around with your program - You
Ctrl-C
to kill the program - Go back to step one
What’s a little inefficient is that to run your new version of a program you
have to issue two commands: :r
and :main
.
We can do better though, ghci
lets you define your custom commands.
Here’s the incantation:
ghci> :def g \_ -> return ":r\n:main"
Now you can just:
ghci> :g
and it will :reload
and run :main
for you.
It’s not smart enough to understand when :reload
fails, but that’s OK because
if :reload
fails there’s no main
and :main
fails as well.