##### Implementation of flatMap() for State transition

2022-05-17 03:27:20    分类:技术分享    scala   functional-programming   monads   state-monad   flatmap

##### Managing state - chapter 3 of SICP

2022-05-03 13:09:36    分类:技术分享    haskell   state   sicp   state-monad

##### Implementation of flatMap() for State transition

Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala, p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following solution: def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => { val (a, r1) = f(rng) g(a)(r1) // We pass the new state along } Stackoverflow provides many answers to flatMap()/monad questions, but none which for me answered my questions regarding the next to last line of code. I do not understand the syntax of the line g(a)(r1) (1) I do not

2022-04-27 15:45:13    分类:问答    scala   functional-programming   monads   state-monad   flatmap

##### Managing state - chapter 3 of SICP

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

2022-04-26 14:53:57    分类:问答    haskell   state   sicp   state-monad

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

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 -> (freq.getOrElse(s, 0) + 1) (freq + tuple, s) } type StateMap[x] = State[Map[Int,Int], x] val

2022-04-26 14:33:21    分类:问答    scala   scalaz   monad-transformers   state-monad

##### What is the difference between `ioToST` and `unsafeIOToST` from GHC.IO

2022-04-19 16:42:05    分类:技术分享    haskell   io   ghc   state-monad

##### Adjoint functors determine monad transformers, but where's lift?

I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three \$ fmap (>>= counit . fmap (getThree . f)) (getThree m) instance (Adjoint f g, Monad m) => Applicative (Three g f m)