我試圖採用ExceptT a (StateT A M),對於某些具體類型A和monad M,並將它們包裹到我的新自定義monads中。 首先我確定了StateT A M在其他環境中經常出現,因此我決定這將是最好的單獨包裝在一個單子M1,然後包裝成ExceptT a M1M2。 期望的特性是使MonadStateM1和M2實例和類M(讓我們假設它被稱爲MyMonadClass)。 M2也應該是Mon
所以我有這樣的代碼 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
import MonadA
data A = A
newtype MonadA a => MyStateT a b { runMyStateT :: StateT A a b }
deriving (Functor, Applicative, Monad, MonadI
我與單子變壓器行使本http://www.cs.nott.ac.uk/~nhn/MGS2006/LectureNotes/lecture03-9up.pdf 提出我已經爲錯誤變壓器它讀起來就像一個實現: newtype ET m a = ET (m (Maybe a))
deriving instance Show (m (Maybe a)) => Show (ET m a)
(其實這在我看
我想基本上映射一個列表並同時攜帶一些狀態。我想結合名單和國家monads可能會讓我在那裏。我嘗試了幾件事情,發現我可能需要使用ListT。作爲我的實際問題的簡化版本,假設我想實現sum函數,同時還返回原始列表的修改版本。這或類似的是我想象它會看起來像: sum' :: ListT (State Int) Int
sum' = do
lift $ put 0
x <- [1,2
我有一個任務來實現一個函數,該函數重複詢問用戶輸入密碼並說明它是否不正確。如果密碼正確,則表示「存儲在數據庫中並退出」。我需要使用ErrorT單子轉換告訴用戶,他的密碼不正確,所以基本的行爲看起來像這樣 GHCi>runErrorT askPassword'
Enter your new password:
qwerty
Incorrect input: password is too sh
有時我需要使用幾個嵌套MonadTrans。例如,我會放一個MaybeT內ExceptT的,以模擬天生continue和break在命令式編程: runExceptT . forM [1..] $ \ _ -> runMaybeT $ do
...
mzero -- this mimics continue
lift $ throwE "..." -- this mi
假設我有andThen由幾個功能Int => Int: val f1: Int => Int = _ + 1
val f2: Int => Int = _ + 2
val f3: Int => Int = _ + 3
val f = f1 andThen f2 andThen f3
現在我還需要返回中間結果。所以我可以將所有這些函數轉換爲Int => (List[Int], Int),
我目前正在玩Bryan O'Sullivan的resource-pool庫,並且有關於擴展withResource函數的問題。 我想將withResource函數的簽名從(MonadBaseControl IO m) => Pool a -> (a -> m b) -> m b更改爲(MonadBaseControl IO m) => Pool a -> (a -> m (Bool, b)) ->
我想將以下狀態命令式代碼轉換成Haskell。 while (true) {
while (get()) {
if (put1()) {
failImmediately();
}
}
if (put2()) {
succeedImmediately();
}
}
無論是put1和put2讀取系統的狀態,並對其進