Questions tagged [state-monad]

A monad allowing state information to be attached to calculations

A state monad allows a programmer to attach state information of any type to a calculation. Given any value type, the corresponding type in the state monad is a function which accepts a state, then outputs a new state along with a return value. Wikipedia has a brief overview.

399 questions
0
votes
1 answer

How to use the State Monad

I have already asked a question about understanding the state monad , but now i felt this one should be a different one. Given i implemented the state monad, i want my state to be a data structure that resembles the Environment: Environment data…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0
votes
1 answer

How to supply generic type for the state-monad's method

I am trying to create a function for the state monad that can receive a generic type in the a : newtype State s a=State { runstate::s->(s,a) } I want to bind only the first type of the runstate::s->(s,a) , and later decide what a should be : So…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0
votes
1 answer

How to understand evalState in this State Monad Haskell code snippet?

I am looking at this compiler code snippet and do not understand what evalState does, being new to State Monad. compileToAst :: FilePath -> String -> Either Errors (Contract (Check Type, Env, SourcePos)) compileToAst source code = case parse parser…
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
0
votes
1 answer

How let State works with Kleisli?

I'm having an example for a logger wrapped with State monad: val logger = Logger(LoggerFactory.getLogger(this.getClass)) def logState[A](s:IO[Unit], a:A): State[List[IO[Unit]], A] = State[List[IO[Unit]], A]{ logs => (logs :+ s, a) …
user3593261
  • 560
  • 4
  • 17
0
votes
1 answer

Control.ST pure type

pure : (result : ty) -> STrans m ty (out_fn result) out_fn from http://docs.idris-lang.org/en/latest/st/state.html#strans-primitive-operations I'm not sure what (out_fn result) out_fn means. Is it about constraining out_fn to be a function of…
corazza
  • 31,222
  • 37
  • 115
  • 186
0
votes
3 answers

how to find the first recurrring character in a string using java 8 stream without intermediate terminal operation

I used the below example code only to illustrate the problem with Java 8 stream API. I am not expecting workaround to the given code but the explanation why accumulator function is not possible/provided in Stream. I want to get the first…
seenimurugan
  • 464
  • 4
  • 19
0
votes
1 answer

Why do I need so much memory for WriterT State?

Trying to get to grips with the concepts I am trying to solve an exercise in Haskell using WriterT and State (it's advent of code day 15). For some reason I do not understand I end up using loads of memory and my notebook (just 4G Ram) comes to a…
bdecaf
  • 4,652
  • 23
  • 44
0
votes
1 answer

Wrapping a class with side-effects

After reading chapter six of Functional Programming in Scala and trying to understand the State monad, I have a question regarding wrapping an side-effecting class. Say I have a class that modifies itself in someway. class SideEffect(x:Int) { …
0
votes
0 answers

Getting state in a stateful monad supporting exceptions

I'm trying to figure out how to solve the following problem: I want a monad that behaves like State and Except, so I wrote something like this: type ExceptStateM s m a = ExceptT String (StateT s m) a Suppose we don't want to wrap another monad and…
Jytug
  • 1,072
  • 2
  • 11
  • 29
0
votes
1 answer

Hiding nested state transformers with newtype in haskell

I'm not sure what I want to accomplish is sane or not (please be nice). But i had an idea for a small game, the game will need to have some state and the state is updated with some random component (otherwise it would be kind of boring). Seeing as…
Patrik
  • 255
  • 3
  • 11
0
votes
1 answer

Applicative instance requires 1 AND 0 arguments

I was going through "Learn Yourself a Haskell" and am trying to implement the "Heathrow to London" problem [1] in a Monadic way (instead of folds or explicit recursions). [1]…
hyiltiz
  • 1,158
  • 14
  • 25
0
votes
1 answer

Exporting a polymorphic MonadState function for a particular state data type

What I'm trying to do is (in a module I'm writing) export a function that works on a particular type in a state monad (in the example below, that type would be Foo). However I would like the user to be able to use the function in whatever MonadState…
jberryman
  • 16,334
  • 5
  • 42
  • 83
0
votes
1 answer

compiler error message when using State monad for memoization

I have a problem to make a working version of the Euler project problem 31 with the use of State trait (inspired from scalaz) First, I have a solution with a mutable HashMap for memoization. It works but i would like to use the State monad, to…
volia17
  • 938
  • 15
  • 29
0
votes
0 answers

Monad Challenges: No instance for MonadState

I'm new to Haskell and trying my hand at the monad challenges. The following code gives an error message: type Rand a = StateT Seed Identity a hi :: Rand Integer hi = get >>= \std -> (put . snd $ rand std) >> return . fst $ rand std -- hi =…
ndrue
  • 69
  • 1
  • 7
0
votes
0 answers

Parser Error Reporting deriving the right instances

I am trying to build an error reporting parser in haskell. Currently I have been looking at a tutorial and this is what I have so far. type Position = (Int, Int) type Err = (String, Position) newtype Parser1 a = Parser1 {parse1 :: StateT String…
Yusuf
  • 491
  • 2
  • 8