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

  • Home
  • SEARCH
  • 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 7840839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:56:13+00:00 2026-06-02T15:56:13+00:00

I have a type that represents game state for my application, for this question

  • 0

I have a type that represents game state for my application, for this question pretend it is something simple like:

Game { points :: Int }

I define my game logic with the State monad.

type GameState a = StateT Game a

addPoints :: Int -> GameState ()
addPoints num = do
    Game p <- get
    put $ Game (p+num)

I want to be able to simply discard some inputs

evenResult num = do
    Game p <- get
    return $ even (p + num) 

addPoints num = do
    isEven <- evenResult num
    if isEven then return () else do
    Game n <- get
    put $ Game (n+num)

I want a syntax that looks something like this

addPoints num = do
    guard evenResult
    ...

-- or this
addPoints num = do
    guardIsEvenResult
    ...

If it hits the guard, I want it to leave the state alone, and do nothing else in the block.

How can I do it? It seems close to possible with MonadPlus, but I’m not sure I could use mzero to say “return what you already have in your state”. Thanks!

  • 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-02T15:56:16+00:00Added an answer on June 2, 2026 at 3:56 pm

    Import Control.Monad.Trans.Maybe and use MaybeT on top of StateT. Then, you can use mzero to abort the computation or, like Kevin Ballard says, guard condition to stop if condition is False; all you have to do is enclose each block in runMaybeT. (Note that you’ll either have to lift every option you have defined on your StateT monad, or change their types to work with any monad with the state you require, like operation :: (MonadState m Game) => ....)

    Note that you probably have the transformers package that Control.Monad.Trans.Maybe is contained in, even if you’re not using it directly; the mtl package, which contains the standard monad modules like Control.Monad.State, depends on it.

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

Sidebar

Related Questions

I have a type that represents a type of number. In this case, I'm
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a type with about 40 properties (all value types) that represents a
Does Java have a data type that represents a period of time eg 34
I have an instance of System.Type that represents an interface, and I want to
I have this table that represents a weak entity and is a typical table
I'm using NHibernate 3.1. I have a user type that represents a System.Drawing.Image .
Assuming I have a simple class that represents a staff member class Staff {
I currently have a type that I inject into my controllers that's used for
I have a data type that contains a set and a method that expects

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.