state-monad

Is State monad needed/useful in a language with mutable (local) variables (such as Scala)?

怎甘沉沦 提交于 2021-02-07 09:10:55
问题 I understand that in Haskell the State monad is useful because there are no mutable variables (unless we are in the IO monad). However, what is the deal with Scala ? Is State Monad useful in a language where there are mutable variables ? In some sense all the State Monad allows is to use some local mutable variables within the Monad context. For example here: newtype Labeled anytype = Labeled (S -> (S, anytype)) instance Monad Labeled where return contents = Labeled (\st -> (st, contents))

IDs from State Monad in Haskell [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-01-16 19:00:48
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Creating unique labels in Haskell I've got a datatype Person and some input data from which I will create the Persons. I'd like to have each Person have its own ID (let's say integers [0..]). I could do this with recursion, but since I'm doing this in Haskell, I'd like to understand the monads. The State Monad is probably the best for this job, I suppose? The thing is, I don't really understand lots of things:

Mixing Threepenny-Gui and StateT

无人久伴 提交于 2020-01-13 19:29:31
问题 I have a question on the interaction of Threepenny-Gui with StateT. Consider this toy program that, every time the button is clicked, adds a "Hi" item in the list: import Control.Monad import Control.Monad.State import qualified Graphics.UI.Threepenny as UI import Graphics.UI.Threepenny.Core hiding (get) main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = void $ do return w # set title "Ciao" buttonAndList <- mkButtonAndList getBody w #+ map element

Zoom instance over Free Monad

若如初见. 提交于 2020-01-05 04:24:10
问题 I'm attempting to build a free monad (using free) which acts just like a StateT monad, but which allows you to also run monads over a base state AppState . I have a separate constructor LiftAction which holds those types. The idea is that you keep zoom ing Actions down until they reach AppState, which can store different states inside its extension map. Here was my earlier (failed) attempt using mtl: Lift through nested state transformers (mtl) Anyways, since it's basically a wrapper over

state monad haskell

痴心易碎 提交于 2020-01-04 06:50:24
问题 I want to write a function for calculating the average using the State Monad in haskell this is the code I wrote as far import Control.Monad.State type MyState = (Double,Double) media s (a,n)= ((a*n+s)/(n+1),n+1) getAverage:: Double ->State MyState s1-> Double getAverage s c=get >>= \s0 -> let (x,s1) =media s s0 in put s1 >> return x I got this error when compile in GHCI, and I stuck there can you help me to understand what is wrong, thank you in advance 回答1: The code you provided gives this

Generalized Newtype Deriving

不问归期 提交于 2020-01-03 10:06:27
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Generalized Newtype Deriving

社会主义新天地 提交于 2020-01-03 10:06:25
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Can I use different workflows simultaneously in F#?

独自空忆成欢 提交于 2019-12-31 04:37:08
问题 I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate

Simulating interacting stateful objects in Haskell

混江龙づ霸主 提交于 2019-12-29 08:27:18
问题 I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable code. My problem is that I don't know how to pull the same trick when I have two (or more) stateful objects interacting with one another. Below I give a highly simplified version of the problem and sketch out what I have so far. For the sake of this

combining StateT with InputT

前提是你 提交于 2019-12-29 08:03:52
问题 It is a follow-up to this question. I'm trying to combine shell from @ErikR's answer in my InputT loop. main :: IO [String] main = do c <- makeCounter execStateT (repl c) [] repl :: Counter -> StateT [String] IO () repl c = lift $ runInputT defaultSettings loop where loop = do minput <- getLineIO $ in_ps1 $ c case minput of Nothing -> lift $ outputStrLn "Goodbye." Just input -> (liftIO $ process c input) >> loop getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String) getLineIO