天道酬勤,学无止境

state-monad

Implementation of flatMap() for State transition

问题 练习 6.8,Chiusano 和 Bjarnason, Scala 中的函数式编程,p。 87 询问如何为以下特征实现 flatMap(): trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) 答案键给出了以下解决方案: 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 为 flatMap()/monad 问题提供了许多答案,但对我来说,没有一个回答我关于倒数第二行代码的问题。 我不明白该行的语法 g(a)(r1) (1) 我不明白 g(a)(r1) 如何评估。 (r1) 提供什么句法功能? 我不相信该行没有例示柯里化,因为 g 只接受一个参数:A。 (2) 如果 g(a) 已经返回类型 Rand[B],那么为什么该行不在这里结束? (3) g(a) 返回的 Rand[B] 与第二组括号 (r1) 之间的关系是什么? (4) 如果flatMap()的这个实现的返回类型是Rand[B],等于RNG => (A, RNG),那么箭头右边的圆括号是怎么产生的?

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

Managing state - chapter 3 of SICP

问题 我一直在研究计算机程序的结构和解释,并在 Haskell 中完成练习。 前两章很好(github 上的代码),但第 3 章让我更加努力地思考。 首先讨论管理状态,以银行账户为例。 他们定义了一个函数make-withdraw (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) 这样您就可以执行以下代码: (define w1 (make-withdraw 100)) (define w2 (make-withdraw 100)) (w1 50) 50 (w2 70) 30 (w2 40) "Insufficient funds" (w1 40) 10 我不确定如何在 Haskell 中模拟这一点。 我首先想到了一个使用 State monad 的简单函数: import Control.Monad.State type Cash = Float type Account = State Cash withdraw :: Cash -> Account (Either String Cash) withdraw amount = state

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

State and IO Monads

问题 我一直在尝试围绕单子的概念进行思考,并且一直在尝试以下示例: 我有一个Editor数据类型,它表示文本文档的状态和一些在它上面工作的函数。 data Editor = Editor { lines :: [Line], -- editor contents are kept line by line lineCount :: Int, -- holds length lines at all times caret :: Caret -- the current caret position -- ... some more definitions } deriving (Show) -- get the line at the given position (first line is at 0) lineAt :: Editor -> Int -> Line lineAt ed n = ls !! n where ls = lines ed -- get the line that the caret is currently on currentLine :: Editor -> Line currentLine ed = lineAt ed $ currentY ed -- move the caret horizontally by the specified

2022-04-26 20:25:21    分类:技术分享    haskell   monads   monad-transformers   state-monad

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

Stateful computation with different types of short-circuit (Maybe, Either)

问题 我试图找到将以下有状态命令式代码转换为纯函数表示的最优雅的方法(最好在 Haskell 中使用其 Monad 实现提供的抽象)。 但是我还不擅长使用变压器等组合不同的单子。 在我看来,分析他人对此类任务的看法对自己学习如何做最有帮助。 命令式代码: while (true) { while (x = get()) { // Think of this as returning Maybe something put1(x) // may exit and present some failure representation } put2() // may exit and present some success representation } 当get返回Nothing我们需要执行以继续put2 ,当get返回Just x我们希望x传递给put1并且仅当put1失败或循环时才短路。 基本上put1和put2可能会终止整个事情或移动到以下语句以某种方式改变底层状态。 get可以成功并调用put1并循环,也可以失败并继续put2 。 我的想法是: forever $ do forever (get >>= put1) put2 为什么我要寻找类似的东西是因为(get >>= put1)可以简单地短路,只要get没有任何返回或put1终止。 同样put2终止外循环。 但是

2022-04-22 05:58:21    分类:技术分享    haskell   functional-programming   monad-transformers   state-monad   imperative-programming

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

问题 -- --------------------------------------------------------------------------- -- Coercions between IO and ST -- | A monad transformer embedding strict state transformers in the 'IO' -- monad. The 'RealWorld' parameter indicates that the internal state -- used by the 'ST' computation is a special one supplied by the 'IO' -- monad, and thus distinct from those used by invocations of 'runST'. stToIO :: ST RealWorld a -> IO a stToIO (ST m) = IO m ioToST :: IO a -> ST RealWorld a ioToST (IO m) = (ST m) -- This relies on IO and ST having the same representation modulo the -- constraint on the

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)

2022-04-04 20:37:55    分类:问答    haskell   monads   monad-transformers   state-monad

Stateful computation with different types of short-circuit (Maybe, Either)

I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at combining different monads using transformers and the like. It seems to me, that analyzing other's takes on such tasks helps the best when learning how to do it myself. The imperative code: while (true) { while (x = get()) { // Think of this as returning Maybe something put1(x) // may exit and present some failure representation } put2() // may exit

2022-03-19 00:24:32    分类:问答    haskell   functional-programming   monad-transformers   state-monad   imperative-programming