Chris Stryczynski
Chris Stryczynski

Reputation: 33881

How can I run multiple statements in GHCi?

I'm doing some very simple performance testing of a simple function that I believe has O(n)(squared) performance (or worse).

Currently I'm running multiple statements which is tedious to repeat:

ghci> myfunction 0 100
true
ghci> myfunciton 0 200
true
ghci> myfunction 0 300
true
ghci> :r

Is there a way I can run all four GHCi statements? I can't just combine them using "native" Haskell as I'd like to include the :r (which is a GHCi statement - not exactly Haskell) that gets run at the end.

Upvotes: 1

Views: 349

Answers (3)

chi
chi

Reputation: 116139

You can define a custom GHCi command using :def in this way:

> :def foo (\_ -> return "print 100\nprint 200\n:t length")
> :foo
100
200
length :: Foldable t => t a -> Int

In the returned string, :-commands can be included as well, like :t above.

Upvotes: 4

madgen
madgen

Reputation: 747

One way of doing it is to create a testing suite in your Cabal file in which you place your function calls as tests, then use stack test --file-watch. That recompiles and reruns the tests every time you save a file.

Upvotes: 2

Chris Stryczynski
Chris Stryczynski

Reputation: 33881

One way I've found is creating a separate file:

myfunction 0 100
myfunction 0 200
myfunction 0 300
:r

and then using:

:script path/to/file

Upvotes: 4

Related Questions