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 convert a generic type in Haskell into a String within a Monadic context

In the MVE code below I have tried to create a function collect which is supposed to take a RegModule-monad as argument such as the scanChar and when this scanChar or other RegModule succeeds in scanning a char as seen in its case branch then the…
Piskator
  • 605
  • 1
  • 9
0
votes
0 answers

What values get unwrapped from a Monad with do-notation and what determines this?

I am still a bit new to Monads and have just realized that something I thought I understood can't be the case. In the MVE that follows I have a hard time understanding why next <- scanChar can return just a single Char and bind it to the next.…
Piskator
  • 605
  • 1
  • 9
0
votes
1 answer

Is MutVar# impure in Haskell?

Now I'm currently struggling understanding State ST STRef etc.. First, I investigated Monad itself, s -> (s, a). I failed to get any meaningful relation between s -> (s, a) and "State" , so I went through newSTRef, readSTRef, writeSTRef. They are…
kwonryul
  • 481
  • 3
  • 10
0
votes
1 answer

Implement Scala Cats Traverse for State

I've been writing my own version of Scala Cats (to assist others when learning this library). I've implemented my own versions of most type classes, but am stuck with a custom implementation of Traverse for State. The function to implement is: def…
D Ainslie
  • 33
  • 1
  • 6
0
votes
1 answer

Data constructor not in scope despite copy-pasting exact code and importing module

I'm trying to run one of the most simple examples from the Haskell online book where you implement a stateful function modeling a stack of numbers. The code from the example I'm using is…
kamoe
  • 3
  • 3
0
votes
1 answer

rewriting a monad with newtype in haskell - some clarifications

I have come across the code below and thought I wanted to rewrite it in order to get a firmer grasp of the Monad. As I have tried to do below. It raises several questions about aspects that I apparently haven't understood fully. type R = Int type W…
Piskator
  • 605
  • 1
  • 9
0
votes
1 answer

Trying to use an Int RandomIO val in my chart

I am new to monads in Haskell. I was trying to write a scatter graph with Haskell-chart that would plot 20 points with strong (but not perfect) positive correlation. Example of what I was trying to do. Specifically, I want a list of random doubles…
m1531
  • 11
  • 2
0
votes
1 answer

Breaking the loop

I have the following code sample: data Smth = A | B data MyError = MkMyError data MyState = MkMyState run :: [Smth] -> Either MyError (Maybe Integer) run param = evalState ( foldM ( \acc a -> do res <- go a …
xbalaj
  • 977
  • 1
  • 8
  • 14
0
votes
1 answer

How do you use the StateT Monad Transformers in Haskell?

Given a simple list newtype SomeList = SomeList { items :: [SomeItem] } and a helper function newtype SomeListStateT a = SomeListStateT { runSomeListStateT :: StateT SomeList IO a } deriving (Functor, Applicative, Monad, MonadIO) Can anyone…
Yunis
  • 23
  • 5
0
votes
0 answers

Tail recursion with the State and CPS monads?

I am in the process of creating a simple parsing library in Haskell, which compiles down the parser specification to optimized code using Template Haskell. However, I am trying to figure out what kind of code is the most efficient to optimize to, so…
0
votes
2 answers

Haskell State monad vs state as parameter performance test

I start to learn a State Monad and one idea bother me. Instead of passing accumulator as parameter, we can wrap everything to the state monad. So I wanted to compare performance between using State monad vs passing it as parameter. So I created two…
lukas kiss
  • 381
  • 2
  • 15
0
votes
1 answer

How to define flatmap for state monad-like thing

I'm trying to work out how to define flatmap for my state monad-like thing. class State(Protocol[T_co]): @abstractmethod def __call__(self: S, i: int) -> tuple[T_co, S]: ... A State encapsulates the state. Here's unit def unit(t: T)…
joel
  • 6,359
  • 2
  • 30
  • 55
0
votes
1 answer

Get result and state instead of just state from State Monad

I have this code: runState (do { put 5; return 'X' }) 1 and the effect is: ('X',5). I would like to extract both result - 'X' and current state - 5 in a do block, then do something with them. However when I use the get function like: z <- get it…
tyecannon
  • 41
  • 3
0
votes
2 answers

Lifting a complete monadic action to a transformer (>>= but for Monad Transformers)

I looked hard to see if this may be a duplicate question but couldn't find anything that addressed specifically this. My apologies if there actually is something. So, I get how lift works, it lifts a monadic action (fully defined) from the…
0
votes
0 answers

Deciding what's state and what's content in State Monad

I've been reading and watching material on FP for the past few weeks and now I'm trying to apply a simple State Monad as an example. I'm programming in Javascript (I know, it's not Haskell..), and I'm struggling with this simple example, so I guess…