state-monad

Managing state - chapter 3 of SICP

孤街醉人 提交于 2019-11-30 12:24:28
I've been working through in Structure and Interpretation of Computer Programs and completing the exercises in Haskell. The first two chapters were fine (code at github ) but Chapter 3 is making me think harder. It starts by talking about managing state, with the example of a bank account. They define a function make-withdraw by (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) so that you can execute the following code: (define w1 (make-withdraw 100)) (define w2 (make-withdraw 100)) (w1 50) 50 (w2

State Monad, sequences of random numbers and monadic code

走远了吗. 提交于 2019-11-30 12:03:02
问题 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 of Bool s for simplicity): type Seed = Int random :: Seed -> (Bool, Seed) random seed = let (a, c, m) = (1664525, 1013904223, 2^32) -- some params for the LCG

Basic Scalaz State question

青春壹個敷衍的年華 提交于 2019-11-30 08:46:42
How do I use State to mimic the behaviour of List.zipWithIndex ? What I have come up with so far (which doesn't work) is: def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match { case x :: xs => (init[Int] <* modify((_:Int) + 1)) map { s : Int => (x -> s) :: (numberSA(xs) ! s) } case Nil => state( (i : Int) => i -> nil[(A, Int)] ) } This is based very loosely on the state example . As I said, it does not work: scala> res4 res5: List[java.lang.String] = List(one, two, three) scala> numberSA(res4) ! 1 res6: List[(String, Int)] = List((one,1), (two,1), (three,1)) I can get it

Tidying up Monads - turning application of a monad transformer into newtype monad

本秂侑毒 提交于 2019-11-30 03:07:00
问题 I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by

State Monad, sequences of random numbers and monadic code

孤者浪人 提交于 2019-11-30 01:55:19
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 of Bool s for simplicity): type Seed = Int random :: Seed -> (Bool, Seed) random seed = let (a, c, m) = (1664525, 1013904223, 2^32) -- some params for the LCG seed' = (a*seed + c) `mod` m in (even seed', seed') -- return True/False if seed' is even/odd Don't

Use of Haskell state monad a code smell?

↘锁芯ラ 提交于 2019-11-29 19:05:06
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 to maintain "state"-ish data as I traverse the syntax tree. For example, when compiling flow-control statements I need to generate unique names for the labels to jump to (labels generated from a counter that's passed in, updated, & returned, and the old value of

Simulating interacting stateful objects in Haskell

淺唱寂寞╮ 提交于 2019-11-29 13:29:35
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 question, let's assume a machine's internal state consists only of a single integer register, so that its

Basic Scalaz State question

半腔热情 提交于 2019-11-29 13:10:18
问题 How do I use State to mimic the behaviour of List.zipWithIndex ? What I have come up with so far (which doesn't work) is: def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match { case x :: xs => (init[Int] <* modify((_:Int) + 1)) map { s : Int => (x -> s) :: (numberSA(xs) ! s) } case Nil => state( (i : Int) => i -> nil[(A, Int)] ) } This is based very loosely on the state example. As I said, it does not work: scala> res4 res5: List[java.lang.String] = List(one, two, three)

combining StateT with InputT

穿精又带淫゛_ 提交于 2019-11-29 11:56:07
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 ios = do s <- liftIO ios getInputLine s And getting an error Main.hs:59:10: Couldn't match type

scalaz List[StateT].sequence - could not find implicit value for parameter n: scalaz.Applicative

那年仲夏 提交于 2019-11-29 11:55:45
问题 I'm trying to figure out how to use StateT to combine two State state transformers based on a comment on my Scalaz state monad examples answer. It seems I'm very close but I got an issue when trying to apply sequence . import scalaz._ import Scalaz._ import java.util.Random val die = state[Random, Int](r => (r, r.nextInt(6) + 1)) val twoDice = for (d1 <- die; d2 <- die) yield (d1, d2) def freqSum(dice: (Int, Int)) = state[Map[Int,Int], Int]{ freq => val s = dice._1 + dice._2 val tuple = s ->