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 7445581
In Process

The Archive Base Latest Questions

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

I am trying to create an agent-like object, that, given an two indices (agent

  • 0

I am trying to create an “agent-like” object, that, given an two indices (agent index, alternative index) returns it’s preference for that alternative.

My error is:

Couldn't match expected type `Integer'
            with actual type `RandomAgent'
In the first argument of `preference', namely `agentNum'
In the expression: preference agentNum alternative
In an equation for `score':
    score agentNum alternative = preference agentNum alternative

What I am trying to have is a piece of code that essentially represents a mathematical function such as A_i(x_j) where A_i is the agent’s “score” function for the alternative x_j.

I would appreciate any hints on how to proceed. Maybe there is a simpler approach…

Full code below for signatures of other functions

module AgentGenerator where

import System.Random

type Alternative = Integer


data RandomAgent = RandomAgent

class Agent a where
    score :: a -> Alternative -> Double

instance Agent RandomAgent where
    score agentNum alternative = preference agentNum alternative

-- TODO: Replace Doubles with Agents
-- Generate `n` agents with `x` alternatives each
generate :: Integer -> Integer -> [Double]
generate agents alternatives = [ preference i j   | i <- [0..(agents-1)], j <- [0..(alternatives-1)] ]

-- Given agent's index and alternative's index return that agent's
-- preference for that alternative
preference :: Integer -> Integer -> Double
preference agent alternative = randomFromSeed $ fromCoord (agent, alternative)

-- Given grid position convert it to a single integer
fromCoord :: (Integer, Integer) -> Integer
fromCoord (agent, alternative) = (agent * 10^0) + (alternative * 10^1)

-- Generate random value between [0, 1] based on the seed
randomFromSeed :: Integer -> Double
randomFromSeed seed = value
    where (value, gen) = randomR (0.0, 1.0) $ mkStdGen (fromIntegral seed)
  • 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:52:13+00:00Added an answer on May 29, 2026 at 11:52 am
    class Agent a where ...
    

    You define a class called Agent…

    instance RandomAgent Integer where ...
    

    …then you declare that Integer implements the RandomAgent class.

    Do you mean to declare a type called RandomAgent, containing an Integer field, and implementing the Agent class? You do that in two steps:

    data RandomAgent = RandomAgent Integer   -- first declare your RandomAgent type
    
    instance Agent RandomAgent where         -- then declare that it implements your Agent class
        score agent alternative = preference agent alternative
    

    (As you’re still thinking in OOP terms, I am obliged to point out that you may not need a class (and if you only have a single type implementing your class, you certainly don’t)… but if you do, that’s how you do it.)


    Answer the second:

    Your preference function expects an Integer as its first parameter, but is actually given a RandomAgent by score. One or other of the functions is going to have to deconstruct (think “unwrap”) the RandomAgent and extract the Integer field within.

    You might do it in your score function like this:

    instance Agent RandomAgent where
        score (RandomAgent agentNum) alternative = preference agentNum alternative
    

    OR you might do it in your preference function like this:

    preference :: RandomAgent -> Integer -> Double
    preference (RandomAgent agent) alternative = randomFromSeed $ fromCoord (agent, alternative)
    

    Obviously, don’t make the changes in both functions, otherwise you’ll find yourself passing an Integer where a RandomAgent is required.

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

Sidebar

Related Questions

I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create a date object from a string. I get date
I'm trying to create a page that tracks some basic user statistics in a
I'm trying to create an automated job for the SQL Server Agent to run.
I'm trying to create a mapping table between two generic (content_type) references, one for
I want to create a web page that has two links. One that downloads

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.