2012-02-10 134 views
1

我想将包含数据(alternatives,agents)和函数的单个文件程序转换为带有数据(和主程序)的两个文件程序,以及仅包含函数的另一个文件程序。如何将变量传递给模块?

但是,在将我的单个文件分成两部分后 - 我必须为所有函数添加其他参数,因为我不再有权访问全局变量alternativesagents。有没有办法将这些变量以某种方式传递到第二个文件?或者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] 

回答

5

我建议你把变量自己的第三个模块中,从GroupPreference导入,并分离出你main功能到一个新的Main.hs文件(这是把main惯用的地方;当你离开模块声明时,它实际上默认为Main,并且文件通常应该以它们定义的模块命名)。

另一种替代方法是将所有函数重写为使用monad,抽象出参数传递 - 这会迫使您重构代码,并且可能是过度杀伤的,除非您想在运行时更改参数。

+0

谢谢!哇,你有13k真的很快。恭喜,并感谢帮助我们凡人。 – drozzy 2012-02-10 20:19:15