gpt4 book ai didi

haskell - 单子(monad)和 ZipList 以外的应用仿函数?

转载 作者:行者123 更新时间:2023-12-03 21:11:56 24 4
gpt4 key购买 nike

应用程序的两个著名示例是 monad 和 ziplist。还有其他例子吗?

最佳答案

来自 Time flies like an applicative functor通过康纳麦克布莱德:

Structure cops will note that De is another example of an applicative functor which is not a monad — join would bring things from the far future to the near future, and that had better not be possible. However, where applicative functors in general only pull through traversable functors (containers with finitely many elements), De pulls through all containers. So it’s a bit special. I wonder what it is.



The De functor represents a fixed delay, rather than an arbitrary one. I’m dividing time into discrete slices. De x is the type of an x due at the next slice. De (De x) is thus the type of an x due in two slices’ time, and you can’t make it turn up any sooner!


阅读整篇文章。为了回答直接的问题,作者的结论是

Don’t Look!

OK, here’s the implementation. It’s a con.

newtype De x = De x deriving Show -- ssh, don't tell!

instance Functor De where
fmap f (De x) = De (f x)

instance Applicative De where
pure = De
De f <*> De s = De (f s)

fix :: (De x -> x) -> x
fix f = f (De (fix f))

关于haskell - 单子(monad)和 ZipList 以外的应用仿函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2120509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com