Haskell 在一个文件中导出多个模块
我需要在一个Haskell文件中导出两个模块。现在,我有Haskell 在一个文件中导出多个模块,haskell,import,module,Haskell,Import,Module,我需要在一个Haskell文件中导出两个模块。现在,我有 module name (important,functions) where module nameForTesting where -- the code is here 但是,它给了我这个错误: filename.hs:5:1: error: parse error on input ‘module’ 如何解决此问题?您需要区分定义模块和从该模块导出名称;这是两个独立的步骤 一个文件只能定义一个模块。但是,您可以将从其他模块导
module name (important,functions) where
module nameForTesting where
-- the code is here
但是,它给了我这个错误:
filename.hs:5:1: error: parse error on input ‘module’
如何解决此问题?您需要区分定义模块和从该模块导出名称;这是两个独立的步骤 一个文件只能定义一个模块。但是,您可以将从其他模块导入的名称作为该模块的一部分导出。比如说,
module MyModule (foo, bar) where
import OtherModule (bar) -- Let's say bar :: Int -> String
foo :: Int -> Int
foo x = x + 3
MyModule
未定义bar
;相反,它从OtherModule
导入它,然后将其作为自身的一部分导出。MyModule
的用户可以访问bar
,而无需显式导入OtherModule
:
import MyModule
main = putStrLn (bar (foo 9))
据我所知,同一个文件中不能有多个模块。似乎证实了这一点 但是,您可以创建第二个模块,用于重新导出某些函数。因此,我们首先创建一个文件
NameForTesting.hs
,其中包含:
-- NameForTesting.hs
module NameForTesting where
important :: Int
important = 42
functions :: Int -> Int
functions = (42 +)
foo :: Int
foo = 21
然后,我们可以构造第二个文件Name.hs
,该文件导入Name用于测试
模块,但仅导出重要
和函数
:
-- Name.hs
module Name(important, functions) where
import NameForTesting
--Name.hs
模块名称(重要,功能),其中
导入名称进行测试
名称
模块在此仅导出从名称测试
模块导入的重要
和功能
。文件属于一个模块。但是,您可以重新导入模块并导出子集。我不确定我是否理解。你能解释一下你的意思吗?