haskell - Haskell:无法弄清楚如何使用monad变压器堆栈

原文 标签 haskell monads monad-transformers

Haskell: Can't figure out how to use monad transformer stack

I have a type

type T s r a = StateT [s] (ListT (Reader r)) a

I'd like to create a monadic value that yields each of the current states

states :: T a r a

But I'm feeling stupid today, and I can't figure out how to transform a value of T a r [a] to T a r a.

states = do
  as <- get
  -- insert magic
  return a
Answer

Figured a way out

states = do
  as <- get
  a <- lift . ListT $ return as
  return a

Which can be compacted down to

states = get >>= lift . ListT . return

ok, feel smarter now

翻译

我有一个类型

type T s r a = StateT [s] (ListT (Reader r)) a


我想创建一个能产生当前状态的单子值

states :: T a r a


但是我今天感觉很愚蠢,我不知道如何将T a r [a]的值转换为T a r a

states = do
  as <- get
  -- insert magic
  return a
最佳答案
想出了办法

states = do
  as <- get
  a <- lift . ListT $ return as
  return a


可以压缩到

states = get >>= lift . ListT . return


好吧,现在变得更聪明
相关推荐

haskell - Haskell —如何使用新的四参数准引号

haskell - xmonad帮助。我的Mod键是哪个?

haskell - 如何为箭头定义过滤器功能?

arrays - 多维可变向量:可能吗?如果是这样,怎么办?

haskell-platform - HDBC -odbc与Haskell连接

haskell - 控制线程退出haskell应用程序

graphics - 树的可视化和动画

haskell - 为什么我不能在GHCi中定义类型?

haskell - Haskell:坚持写一个函数来复制`elem`

haskell - Haskell模块:隐藏的名称和ghci