How to load a script in ghci?

I am just starting to learn Haskell and have difficulty understanding the "flow" of Haskell.

For example, in Python, I can write a script, load it into the interpreter, and see the results:

def cube(x): return x*x*x print cube(1) print cube(2) print cube(cube(5)) # etc... 

In Haskell, I can do this:

 cube x = x*x*x main = print (cube 5) 

Download it with runhaskell and it will print 125 .
Or I could use ghci and manually type in all the functions I want to test

But I want to use my text editor, write some functions, some tests, and Haskell print some results:

 -- Compile this part cube x = x*x*x -- evaluate this part: cube 1 cube 2 cube (cube 3) --etc.. 

Is this possible?

+7
source share
3 answers
 cube x = x*x*x main = do print $ cube 1 print $ cube 2 print $ cube (cube 3) 
 $ ghci cube.hs ... ghci> main 

See the GHCI User Guide .


I also recommend checking out the QuickCheck library.

You will be amazed at how amazing testing can be.

+7
source

Very possible!

 $ ghci > :l filename.hs 

This will download the file, and then you can use the functions directly.

 > :r 

This will reload the file after making the changes. No need to mention the file, it will reload everything that was last loaded. This will also work if instead of :l instead of ghci filename.hs execute ghci filename.hs .

+14
source

To load the Haskell source file into GHCi, use the command :load

cf Download source file to Haskell documentation

+4
source

All Articles