state-monad

Use of Haskell state monad a code smell?

God I hate the term "code smell", but I can't think of anything more accurate. I'm designing a high-level language & compiler to Whitespace in my spare time to learn about compiler construction, language design, and functional programming (compiler is being written in Haskell). During the code generation phase of the compiler, I have t...

How does 'get' actually /get/ the initial state in Haskell?

I have a function: test :: String -> State String String test x = get >>= \test -> let test' = x ++ test in put test' >> get >>= \test2 -> put (test2 ++ x) >> return "test" I can pretty much understand what goes on throughout this function, and I'm starting to get the hang of monads. What I don't understand is how...

How can I initialize state in a hidden way in Haskell (like the PRNG does)?

Hi, I went through some tutorials on the State monad and I think I got the idea. For example, as in this nice tutorial: import Data.Word type LCGState = Word32 lcg :: LCGState -> (Integer, LCGState) lcg s0 = (output, s1) where s1 = 1103515245 * s0 + 12345 output = fromIntegral s1 * 2^16 `div` 2^32 getRandom :: State LCG...

State Monad, sequences of random numbers and monadic code

I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad, not build a good RNG library). The generator is just this (I want to generate a sequence...

Haskell Monad Transformer Stack and Type Signatures

I am attempting to create a stack of monad transformers and am having trouble getting the correct type signatures for my functions. (I'm still pretty new to Haskell) The stack combines multiple StateT transformers since I have multiple states I need to keep track of (two of which could be tupled, but I'll get to that in a second) and a ...

Haskell: How to write interactive interpreter on top of a State monad?

We're working on a model filesystem that uses a state monad internally. We have a type class with operations like these: class Monad m => FS m where isDirectory :: Path -> m Bool children :: Path -> m [Path] ... We're working on a little interactive interpreter that will offer commands like cd, ls, cat, and so on. An oper...

Tips for more elegant code with monads?

Hi. I finally got a hold on how to use monads (don't know if I understand them...), but my code is never very elegant. I guess is from a lack of grip on how all those functions on Control.Monad can really help. So I'd thought it would be nice to ask for tips on this in a particular piece of code using the state monad. The goal of the co...