Creative uses of monads

后端 未结 8 796
再見小時候
再見小時候 2020-12-07 07:39

I\'m looking for creative uses of monads to learn from. I\'ve read somewhere that monads have been used for example in AI, but being a monad newbie, I fail to see how.

8条回答
  •  甜味超标
    2020-12-07 08:14

    I'd like to list a couple of monads not yet mentioned in other answers.

    Enumerate and weighted search monads

    The Omega monad can be used to productively traverse infinite lists of results. Compare:

    >>> take 10 $ liftM2 (,) [0..] [0..]
    [(0,0),(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),(0,8),(0,9)]
    
    >>> take 10 $ runOmega $ liftM2 (,) (each' [0..]) (each' [0..])
    [(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(0,3),(1,2),(2,1),(3,0)]
    

    With a bit more advanced WeightedSearch monad it is also possible to assign weights to computations so that results of computations with lower weights would appear first in the output.

    Accumulating errors monad

    A useful These data type forms a Monad similar to Either, but able to accumulate errors rather. The package also defines MonadChronicle class as well as ChronicleT monad transformer based on These.

提交回复
热议问题