- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确定我一定遗漏了什么。
我是 Haskell 的新手,曲线非常陡峭。在我的玩具项目中,我真的很想使用 State monad 来避免在任何地方传递一千个参数。我无法理解如何将 State monad 从 IO 传递到纯代码中。概念上是这样的(除了 StateT 而不是 ExceptT):
import Control.Monad.Except
import Control.Monad.Identity
type PlayM = Except String
type PlayMIO = ExceptT String IO
puree :: String -> PlayM String
puree = return . ("bb"++)
impuree :: String -> PlayMIO String
impuree s = do
a <- return $ runIdentity $ runExceptT $ puree s
return $ "aa" ++ a
main = do
runExceptT $ impuree "foo"
putStrLn "hi"
play.hs:15:20:
Couldn't match expected type ‘[Char]’
with actual type ‘Either String String’
In the second argument of ‘(++)’, namely ‘a’
In the second argument of ‘($)’, namely ‘"aa" ++ a’
最佳答案
你很接近。让我们遵循带有类型孔的类型(_
s):
impuree :: String -> PlayMIO String
impuree s = do
a <- _ . runIdentity . runExceptT $ puree s
return $ "aa" ++ a
Test.hs:15:8:
Found hole ‘_’
with type: m0 (Either String String) -> ExceptT String IO [Char]
Where: ‘m0’ is an ambiguous type variable
Relevant bindings include
s :: String (bound at Test.hs:13:9)
impuree :: String -> PlayMIO String (bound at Test.hs:13:1)
In the first argument of ‘(.)’, namely ‘_’
In the expression: _ . return . runIdentity . runExceptT
In a stmt of a 'do' block:
a <- _ . return . runIdentity . runExceptT $ puree s
m (Either e b)
的东西。成
ExceptT e m b
:
ExceptT :: m (Either e b) -> ExceptT e m b
impuree :: String -> PlayMIO String
impuree s = do
a <- ExceptT . return . runIdentity . runExceptT $ puree s
return $ "aa" ++ a
ExceptT . f . runExceptT
用函数抽象出来
mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b
m
是
Identity
和
n
是
IO
.使用这个,我们得到:
impuree :: String -> PlayMIO String
impuree s = do
a <- mapExceptT (return . runIdentity) $ puree s
return $ "aa" ++ a
mmorph
的包。这使得与 monad 态射(从一个 monad 到另一个 monad 的转换)一起工作变得更好。这个包有一个功能
generalize :: Monad m => Identity a -> m a
我们可以使用:
impuree :: String -> PlayMIO String
impuree s = do
a <- mapExceptT generalize $ puree s
return $ "aa" ++ a
mmorph
,我们不妨使用更一般的形式:
impuree :: String -> PlayMIO String
impuree s = do
a <- hoist generalize $ puree s
return $ "aa" ++ a
hoist
概括
mapExceptT
对于任何类似 monad 转换器的东西,您可以将 monad 态射应用于底层 monad:
hoist :: (MFunctor t, Monad m) => (forall a. m a -> n a) -> t m b -> t n b
关于haskell - 将 StateT 移入和移出 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32106814/
Monad 转换器很棘手,我不确定(=没有良好的直觉)哪一个应该放在最上面。 最佳答案 StateT s (ExceptT e m) 这说: 以m开头 添加异常(exception)情况 添加状态 现
我无法找出缩放效果(例如 StateT)的最简洁方法,该效果将值返回到矢量或 map 等索引容器中。 例如,假设我有一些纸牌游戏的结构: data Card = Card { cardValu
我正在编写一个作为守护进程运行的程序。 要创建守护进程,用户需要提供一组 每个所需类的实现(其中一个是数据库) 所有这些类都有功能StateT s IO a 形式的类型签名, 但是 s每个类(clas
为了熟悉 monad 转换器,我编写了以下代码: data GlobalState = GlobalState { rng :: StdGen } foo :: IO () foo =
我在玩Cont描述的单子(monad)技巧here在 this SO question . 这个函数让你“跳回”到计算的早期,接受一个参数,这样你就可以做不同的事情: import Control.M
不同的 IDE 都有其怪癖,因此能够知道您正在使用什么 IDE 来运行 R 有时会很有用。 您可以通过测试 RSTUDIO 来测试您是否正在运行 RStudio。环境变量。 is_rstudio <-
我想弄清楚如何实现 MonadBaseControl 的实例对于 Foo 类型,它是 StateT 实例的新型包装器。你会认为它会像 this 一样实现但情况似乎并非如此。我假设状态块是导致这里问题的
我不明白为什么这段代码只循环一次然后退出? 在 Ghci 中,我只能回答第一个循环,然后似乎变量 cont 设置为 false 并且我没有提示回答。 结果是: *Main> testLoop1 td1
我们如何让用户传递一个 eventHandler,它使用 stateMonad 但在单独的线程中调用?比如下面的例子中,应该如何调用forkIO,以便eventHandler可以调用操作呢?我是 Ha
我确定我一定遗漏了什么。 我是 Haskell 的新手,曲线非常陡峭。在我的玩具项目中,我真的很想使用 State monad 来避免在任何地方传递一千个参数。我无法理解如何将 State monad
我在一个 Haskell 应用程序中需要跟踪两个或多个独立状态。 我使用 声明两个新类型类 type MonadTuple m = MonadState (Int, Int) m type Monad
我正在尝试在 "scalaz-core"%"7.2.14" StateT monad 的组合中创建某种故障转移行为。 StateT monad 包裹 EitherT,因此它是一个 monad 转换器:
这个问题有人问过before ,但没有真正的答案。事实上,公认的答案表明这是不可能的,尽管 StateT 是一个 Monad,因此是 Applicative 的超集。因此,标准库只使用 () = ap
我正在学习 Monad Transformers,其中一个练习要求为 StateT 实现 Monad 实例。我想使用 validity 测试我的实现是否符合 Monad 法则包,类似于 checker
我正在尝试制作一个愚蠢的网络服务器,将数据存储为 State .我正在使用 Web.Scotty . I've used ReaderT before with scotty to access co
我正在尝试使用管道和构建在它上面的各种库来编写一个基本的网络服务器。预期的流程是: 从套接字获取字节串 -> 使用二进制解码 -> 服务器逻辑在这里 -> 向套接字发送响应 我认为会是这样的: fro
具体来说,假设我有这个 monadT 堆栈: type MHeap e ret = MaybeT ( StateT [e] Identity ) ret 和一个方便的 runMheap 函数: run
通过保持 IORef 来通过异常来维护状态似乎要容易得多。而不是尝试使用 State Monad。下面我们有 2 个替代的 State Monad。一用StateT和另一个 ReaderT IORef
可以说我有一个功能 f :: State [Int] Int 和一个功能: g :: StateT [Int] IO Int 我想使用 f在 g并传递它们之间的状态。是否有库函数StateT (ret
有人知道是否有任何(简单的)方法可以让 StatEt 使用 R 2.15 吗?当前 StatEt 版本卡住为 R 2.14 版本。 最佳答案 我使用 StatET 3.0 的测试版本和 RJ 1.1,
我是一名优秀的程序员,十分优秀!