简体   繁体   English

我在哪里保存我的Haskell“模块”?

[英]Where do I save my Haskell “modules”?

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. 我在Windows 64位计算机上使用Haskell平台。

I usually put my modules in the same directory tree, and start up ghci at the root directory of the tree. 我通常将我的模块放在同一个目录树中,并在树的根目录下启动ghci Then modules can import each other, and I can easily :load modules into ghci interactively. 然后模块可以相互导入,我可以轻松地:load交互式地将模块:loadghci

$ 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 . 如果您希望从几个不同的项目中访问您的模块,我建议为他们创建一个cabal包并使用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. 不需要发布到hackage - 没有参数的cabal install在当前目录中查找.cabal文件并安装相应的包。

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. 如果您希望从单个项目访问您的模块 - 在分层文件夹树中组织源的通常做法也适用于Haskell。 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. Hello/World.hs应该在头文件中有module Hello.World where The main module should have module Main , but actual file name can be anything (eg Quux.hs ). 主模块应该有module Main ,但实际的文件名可以是任何东西(例如Quux.hs )。 In Foo/Bar.hs you can use import Hello.World . Foo/Bar.hs您可以使用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. 当您在ghci加载Foo/Bar.hs时,当前目录应该是树的根目录,否则它将找不到Hello.World。 You can pass module names instead of file names to ghci: eg ghci Hello.World will work. 您可以将模块名称而不是文件名传递给ghci:例如ghci Hello.World将起作用。

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/separate-compilation.html

http://haskell.org/ghc/docs/latest/html/users_guide/packages.html http://haskell.org/ghc/docs/latest/html/users_guide/packages.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM