对模块的Haskell引用
是否可以引用haskell中的模块?我有几个模块,每个模块都公开相同的接口(两个具有相同名称和签名的函数)。是否可能有这样的模块列表,以调用每个模块的函数 我不确定您想要什么,但名称冲突通常通过使用限定的导入来解决。在下面的示例中,观察对模块的Haskell引用,haskell,module,Haskell,Module,是否可以引用haskell中的模块?我有几个模块,每个模块都公开相同的接口(两个具有相同名称和签名的函数)。是否可能有这样的模块列表,以调用每个模块的函数 我不确定您想要什么,但名称冲突通常通过使用限定的导入来解决。在下面的示例中,观察S.和M.来明确说明函数或类型所在的模块 import qualified Data.Set as S import qualified Data.Map as M mySet :: S.Set Int mySet = S.fromList [1,2,3]
S.
和M.
来明确说明函数或类型所在的模块
import qualified Data.Set as S
import qualified Data.Map as M
mySet :: S.Set Int
mySet = S.fromList [1,2,3]
myMap :: M.Map String Int
myMap = M.fromList [("a", 1), ("b", 2), ("c", 3)]
main = do
print mySet
print myMap
print $ S.member 2 mySet
print $ M.lookup "c" myMap
但从您的问题来看,您似乎在寻找一些元编程解决方案,其中的值是模块,例如
setModule、mapModule::Module
,并在其上使用getter,如getFunctions::Module->[Functions]
。我确信这是不存在的,因为它不适合静态类型的语言。但是,如果您严重滥用模板Haskell或宏,您可能会产生一些问题。我不确定您想要什么,但名称冲突通常通过使用限定的导入来解决。在下面的示例中,观察S.
和M.
来明确说明函数或类型所在的模块
import qualified Data.Set as S
import qualified Data.Map as M
mySet :: S.Set Int
mySet = S.fromList [1,2,3]
myMap :: M.Map String Int
myMap = M.fromList [("a", 1), ("b", 2), ("c", 3)]
main = do
print mySet
print myMap
print $ S.member 2 mySet
print $ M.lookup "c" myMap
但从您的问题来看,您似乎在寻找一些元编程解决方案,其中的值是模块,例如
setModule、mapModule::Module
,并在其上使用getter,如getFunctions::Module->[Functions]
。我确信这是不存在的,因为它不适合静态类型的语言。但是,如果严重滥用模板Haskell或宏,则可能会引发一些问题。为接口定义数据类型怎么样?每个模块提供一个数据类型实例,然后您可以遍历所有实例
-- ModuleInterface.hs
-- Replace types of the functions with actual types
data ModuleInterface = ModuleInterface (Int -> Bool -> Int) (String -> Int)
-- ModuleA.hs
moduleInterface :: ModuleInterface
moduleInterface = ModuleInterface f1 f2
-- Declare f1 and f2
-- ModuleB.hs
moduleInterface :: ModuleInterface
moduleInterface = ModuleInterface f1 f2
-- Declare f1 and f2
-- Main.hs
-- Simple example showing how to "call" all of the functions. If you are doing
-- IO, then you would have to use something like mapM.
transform :: [ModuleInterface] -> Int -> Bool -> String -> [(Int, Int)]
transform interfaces i b s = map f interfaces
where f (ModuleInterface g h) = (g i b, h s)
为接口定义数据类型怎么样?每个模块提供一个数据类型实例,然后您可以遍历所有实例
-- ModuleInterface.hs
-- Replace types of the functions with actual types
data ModuleInterface = ModuleInterface (Int -> Bool -> Int) (String -> Int)
-- ModuleA.hs
moduleInterface :: ModuleInterface
moduleInterface = ModuleInterface f1 f2
-- Declare f1 and f2
-- ModuleB.hs
moduleInterface :: ModuleInterface
moduleInterface = ModuleInterface f1 f2
-- Declare f1 and f2
-- Main.hs
-- Simple example showing how to "call" all of the functions. If you are doing
-- IO, then you would have to use something like mapM.
transform :: [ModuleInterface] -> Int -> Bool -> String -> [(Int, Int)]
transform interfaces i b s = map f interfaces
where f (ModuleInterface g h) = (g i b, h s)