2012-02-10 3 views
1

データ(およびメインプログラム)と機能だけを持つ2つのファイルにデータ(alternativesagents)と関数を含む1つのファイルプログラムを変換したいと考えています。変数をモジュールに渡すには?

しかし、1つのファイルを2つに分割した後、グローバル変数alternativesagentsにアクセスできなくなったため、すべての関数に引数を追加する必要がありました。これらの変数を何とか2番目のファイルに渡す方法はありますか?あるいは、ハスケルに存在するより良い選択肢がありますか?

投稿の長さは申し訳ありませんが、私は完全に私の問題を説明したかったのです。

初期バージョン、1つのファイルに

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

は、私はあなたが、自分自身の第3のモジュールで変数を入れGroupPreferenceからそれをインポートし、mainを置くための慣用的な場所である(新しいMain.hsファイルにあなたのmain機能を分離示唆します。モジュール宣言を省略すると、実際にはデフォルトでMainになり、ファイルは一般に定義されたモジュールの後に名前が付けられます。

もう1つの方法は、Readerモナドを使用するようにすべての関数を書き直し、パラメータ渡しを抽象化することです。これにより、コードを再構成する必要があり、実行時にパラメータを変更しない限り、おそらく過剰です。

+0

ありがとうございます!うわー、あなたは本当に速い13kを持っています。おめでとう、私たちの死体を助けてくれてありがとう。 – drozzy

関連する問題