[英]Is there a patten match with Writer Monad in Haskell?
What is correct way to do pattern match with Writer Monad in Haskell? 在Haskell中与Writer Monad进行模式匹配的正确方法是什么?
I want to do like following... 我想做下面的...
import Control.Monad.Writer
hoge :: Writer [Int] (Int, String) -> String
hoge (Writer (_, (_, s))) = s
Actually, I don't really know difference between Writer
and WriterT
and Writer.Identity
, etc... 其实我真的不知道
Writer
和WriterT
和Writer.Identity
等之间的区别...
Any guidance would be appreciated, Thank you for your reading. 任何指导将不胜感激,谢谢您的阅读。
If I've understood correctly what you're trying to do, you can use runWriter
to get ((Int, String), [Int])
out of your Writer
, and then extract the string from that. 如果我理解正确你想做什么,你可以用
runWriter
得到((Int, String), [Int])
出你的Writer
,然后提取该字符串。 Something like snd . fst . runWriter ...
像
snd . fst . runWriter ...
一样snd . fst . runWriter ...
snd . fst . runWriter ...
snd . fst . runWriter ...
, perhaps. snd . fst . runWriter ...
,也许。
WriterT
is what you use when you need to combine a Writer
with some other monad. 当需要将
Writer
与其他WriterT
结合使用时,可以使用WriterT
。 Rather than writing separate-but-similar implementations for Writer
and WriterT
, it was easier to write just the implementation for WriterT
, and define Writer
as a WriterT
that uses the Identity
monad. 而不是写了分离但类似的实现
Writer
和WriterT
,这是比较容易写只是为了实现WriterT
,并定义Writer
作为WriterT
使用的Identity
单子。 The Identity
monad doesn't really do anything; Identity
Monad并没有真正做任何事情。 you can simply pretend it's not there. 您可以简单地假装它不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.