Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:12:58+00:00 2026-05-29T11:12:58+00:00

I want to convert my single file program – which contains the data (

  • 0

I want to convert my single file program – which contains the data (alternatives, agents) and functions, into two – one with data (and main program) and another with just the functions.

However, after I split my single file in two – I had to add additional arguments to all the functions, since I no longer had access to global variables alternatives and agents. Is there a way to pass those variables into the second file somehow? Or is there a better alternative that exists in Haskell?

Sorry for the length of the post, but I wanted to illustrate my problem in full.

Initial version, in one file

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]

Version in two files

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:12:59+00:00Added an answer on May 29, 2026 at 11:12 am

    I suggest you put the variables in a third module of their own, import it from GroupPreference, and separate out your main function into a new Main.hs file (which is the idiomatic place to put main; when you leave off the module declaration, it actually defaults to Main, and files should generally be named after the module they define).

    Another alternative would be to rewrite all your functions to use a Reader monad, abstracting out the parameter passing — this would force you to restructure your code, and is probably overkill unless you want to vary the parameters at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a single number of bytes, into a file size (that
We've got a file-based program we want to convert to use a document database,
I want to convert a single character of a string into an integer, add
I want to convert index.php file into html file for quick response to client.
If I want to convert a single numeric char to it's numeric value, for
I want to convert a string into a double and after doing some math
I have an enormous file of HTML that I'd like to convert to one
I want to convert the 5 space indentation in a python file to 4
I'm using php to take xml files and convert them into single line tab
I know I can convert a single file encoding under OSX using: iconv -f

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.