I put some functions in a file. Where on my PC should I save this file so that I can easily load my functions?
I am using the Haskell Platform on a Windows 64-bit computer.
I usually put my modules in the same directory tree, and start up ghci
at the root directory of the tree. Then modules can import each other, and I can easily :load
modules into ghci
interactively.
$ ghci
.... loading ....
Prelude> :load directory/subdirectory/mymodule.hs
If you want your modules to be accessible from a few different projects, I'd recommend to create a cabal package for them and install it using cabal install
. Publishing to hackage is not required - cabal install
without arguments looks for .cabal
file in the current directory and installs the corresponding package.
If you want your modules to be accessible from a single project - the usual practice of organizing your sources in a hierarchical folder tree applies to Haskell as well. Let me show an example:
Hello/World.hs
Foo/Bar.hs
Quux.hs
Hello.hs
Hello/World.hs
should have module Hello.World where
in the header. The main module should have module Main
, but actual file name can be anything (e.g. Quux.hs
). In Foo/Bar.hs
you can use import Hello.World
. When you load Foo/Bar.hs
in ghci
, the current directory should be the root of your tree, or else it will not find Hello.World. You can pass module names instead of file names to ghci: e.g. ghci Hello.World will work.
Here are the documentation:
http://haskell.org/ghc/docs/latest/html/users_guide/separate-compilation.html
http://haskell.org/ghc/docs/latest/html/users_guide/packages.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With