我想将我的单个文件程序(包含数据(alternatives
,agents
)和函数转换为两个 - 一个包含数据(和主程序),另一个包含函数。
但是,在将我的单个文件拆分为两个之后 - 我不得不为所有函数添加其他参数,因为我不再能够访问全局变量alternatives
和agents
。有没有办法以某种方式将这些变量传递到第二个文件?或者Haskell中是否存在更好的替代方案?
很抱歉帖子的长度,但我想完整地说明我的问题。
初始版本,在一个文件中
import Data.List
-----------
-- Setup
-----------
alternatives = [1, 2, 3]
agent_1 x = case x of
1 -> 1
2 -> 0.6
_ -> 0.2
agent_2 x = case x of
1 -> 0.4
2 -> 1
_ -> 0.3
agent_3 x = case x of
1 -> 0.2
2 -> 0.3
_ -> 1
agents = [ agent_1, agent_2, agent_3]
-- collaboration imperative
h x = x^2
-----------
-- Program
-----------
-- Sum of scores by agent
s_i agent = sum [agent(x) | x <- alternatives]
-- Sum of all the scores
s agents = sum [s_i agent | agent <- agents]
-- Importance of an agent
im agent = s_i agent / s agents
-- evaulate agent importances and sort them
agent_ims agents = sort [im agent | agent <- agents]
-- agent scores and importances sorted by scores for a particular alternative 'x'
agent_scores :: Integer -> [(Double, Double)]
agent_scores x = sortBy (\(s1,im1) (s2,im2) -> compare s1 s2)
[(agent x, im agent) | agent <- agents]
num = length(agents)
--U for an alternative 'x' and index 'i'
u x i = sum $ drop (num - i) $ [im | (score, im) <- agent_scores x]
-- 'w' for an agent given a particular collaboration imperative h
-- and alternative 'x'
w i x h = h(u x i) - h(u x (i-1))
-- sort according to which agent's score is the greatest for a given -- alternative
b alt = reverse $ sort [agent alt | agent <- agents]
-- zip 'b' with an index variable, to pass to the w
zipped_alt alt = zip [1..] (b alt)
--group pref function - for an alternative 'x'
g x = sum $ [ w i x h * score | (i, score) <- zipped_alt x]
两个文件中的版本
Test.hs
:
import GroupPreference
import Data.List
-----------
-- Setup
-----------
alternatives = [1, 2, 3]
agent_1 x = case x of
1 -> 1
2 -> 0.6
_ -> 0.2
agent_2 x = case x of
1 -> 0.4
2 -> 1
_ -> 0.3
agent_3 x = case x of
1 -> 0.2
2 -> 0.3
_ -> 1
agents = [ agent_1, agent_2, agent_3]
-- collaboration imperative
h x = x^2
main :: IO ()
main = putStrLn $ show $ g 2 h agents alternatives
GroupPreference.hs
:
module GroupPreference
where
import Data.List
-----------
-- Program
-----------
-- Sum of scores by agent
s_i agent alternatives = sum [agent(x) | x <- alternatives]
-- Sum of all the scores
s agents alternatives = sum [s_i agent alternatives | agent <- agents]
-- Importance of an agent
im agent agents alternatives = s_i agent alternatives / s agents alternatives
-- agent scores and importances sorted by scores for a particular alternative 'x'
agent_scores x agents alternatives = sortBy (\(s1,im1) (s2,im2) -> compare s1 s2)
[(agent x, im agent agents alternatives) | agent <- agents]
--U for an alternative 'x' and index 'i'
u x i agents alternatives = sum $ drop (length(agents) - i) $ [im | (score, im) <- agent_scores x agents alternatives]
-- 'w' for an agent given a particular collaboration imperative h
-- and alternative 'x'
w i x h agents alternatives = h(u x i agents alternatives) - h(u x (i-1) agents alternatives)
-- sort according to which agent's score is the greatest for a given -- alternative
b alt agents = reverse $ sort [agent alt | agent <- agents]
-- zip 'b' with an index variable, to pass to the w
zipped_alt alt agents = zip [1..] (b alt agents)
--group pref function - for an alternative 'x'
g x h agents alternatives = sum $ [ w i x h agents alternatives * score | (i, score) <- zipped_alt x agents]
答案 0 :(得分:5)
我建议您将变量放在自己的第三个模块中,从GroupPreference
导入,然后将main
函数分离到新的Main.hs
文件中(这是惯用的)放置main
的地方;当你放弃模块声明时,它实际上默认为Main
,文件通常应该在他们定义的模块之后命名。
另一个替代方法是重写所有函数以使用Reader
monad,抽象出参数传递 - 这会强制你重构代码,除非你想在运行时改变参数,否则它可能有点过分