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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:48:09+00:00 2026-06-15T01:48:09+00:00

The AI code for my little soccer game basically works like this: There is

  • 0

The AI code for my little soccer game basically works like this: There is a function that derives facts that describe the current situation on the pitch:

deriveFacts :: GameState -> [Fact]

… where the facts look kind of like this:

data Fact =
    FactCanIntercept ObjId ObjId
  | FactBestPosition Team Spot Spot
  | FactKickOff
  | FactBallCarrier ObjId
  | FactBestShootingVector Velocity3
  | FactBestPassingVector Velocity3
  | ...

… and there are rules that look uniformly like this:

rule_shoot facts = do
    FactBallCarrier ballCarrier <- checkBallCarrier facts
    FactBestShootingVector goalVector <- checkBestShootingVector facts
    return [message (ballCarrier, shoot goalVector)]

And then there is a rule engine that runs all the rules and gathers up the resulting messages.

That’s fine so far and works quite nicely. What’s kind of annoying, though: In this approach, I need an “accessor” function like this for every Fact:

checkBestShootingVector :: [Fact] -> Maybe Fact
checkBestShootingVector facts = 
   listToMaybe [e | e@(FactBestShootingVector {}) <- facts]

This approach leads to a lot of ugly boilerplate code. My question: Is there an elegant solution that removes the need for creating these accessor functions by hand?

  • 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-06-15T01:48:10+00:00Added an answer on June 15, 2026 at 1:48 am

    You could refactor most of your Fact data as a record object

    data FactRec = FR {
       canIntercept :: [(ObjId,ObjId)], -- use lists for things you previously had multiple times
       factBestPosition :: [(Team,Spot,Spot)], -- not sure about this one, maybe two entries not one list 
       kickOff :: Bool,
       ballCarrier :: ObjID,
       factBestShootingVector :: Velocity3, 
       factBestPassingVector :: Velocity3,
       .....
       }
    

    Which gives you bulti-in accessor functions

    if kickOff fr then something else somethingelse
    

    So you wouldn’t need to write all the check functions, but instead do

    rule_shoot facts = message (ballCarrier facts, shoot $ goalVector facts)
    

    If there are facts that genuinely might or might not be there, they could be of type Maybe something, and if there are facts that can be there an arbitrary number of times, they can be lists.

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

Sidebar

Related Questions

found this little code snippet that seems to do what i want, but im
There is very little code out there that is in VB, and i'm getting
I would like to code a little web site that will contain several sections
I have this little code: $(ul#mainnav > li).hover(function(){ $(ul#mainnav > li > a).slideUp(); })
I have this little code: alert(1); $('input[name^=Quantity_]').each(function () { alert(2); $(this).rules(add, { required: true,
I have this little code: HttpContext.Current.Response.Clear(); if ( files.Contains( ',' ) ) { HttpContext.Current.Response.ContentType
I have this little code here when click a link on webview that is
I would like to write a little code that copy on a local pc
I have this little code to post via ajax: $('#submit').click(function () { var myName
I would like to make my code a little better. This is my array:

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.