monads

Why are Clojure's `let` and `for` both monads?

坚强是说给别人听的谎言 提交于 2019-11-29 08:56:10
问题 In this discussion Brian Marick makes the point that let and for are monads in Clojure: That said, the really general-purpose monads tend to get written into the language as special forms. Clojure's let and for are both monads, but you don't need to know that to use them. This is let user=> (let [c (+ 1 2) [d e] [5 6]] (-> (+ d e) (- c))) 8 This is for user=> (for [x [0 1 2 3 4 5] :let [y (* x 3)] :when (even? y)] y) (0 6 12) My question is: Why are Clojure's let and for both monads? 回答1: Why

Monad with no wrapped value?

核能气质少年 提交于 2019-11-29 08:42:24
问题 Most of the monad explanations use examples where the monad wraps a value. E.g. Maybe a , where the a type variable is what's wrapped. But I'm wondering about monads that never wrap anything. For a contrived example, suppose I have a real-world robot that can be controlled, but has no sensors. Maybe I'd like to control it like this: robotMovementScript :: RobotMonad () robotMovementScript = do moveLeft 10 moveForward 25 rotate 180 main :: IO () main = liftIO $ runRobot robotMovementScript

How to implement a global counter using Monad?

ε祈祈猫儿з 提交于 2019-11-29 07:41:40
问题 I need a global counter, starting from 0, 1, 2, 3, ..... I kind of understand that this "impure" code should be separately implemented... I am just starting to understand Monad, but have no idea how to implement this global counter using Monad? This could be very useful example for understanding if it is possible 回答1: State monad gives you state but only inside the monad. It is not persistent across repeated invocations of the function. If you want truly global, mutable state you might want

How do you stop building an Option[Collection] upon reaching the first None?

偶尔善良 提交于 2019-11-29 07:15:09
When building up a collection inside an Option , each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return None for the whole collection. What is an idiomatic way to do this in Scala? Here's one approach I've come up with: def findPartByName(name: String): Option[Part] = . . . def allParts(names: Seq[String]): Option[Seq[Part]] = names.foldLeft(Some(Seq.empty): Option[Seq[Part]]) { (result, name) => result match { case Some(parts) => findPartByName(name)

Why is there no << in the Haskell standard library?

风格不统一 提交于 2019-11-29 06:06:33
The Monad class defines a >> method, which sequences two monadic actions: >> :: Monad m => m a -> m b -> m b The binding operator >>= has a flipped-argument equivalent, =<< ; as do the monadic function composition ('fish') operators >=> and <=< . There doesn't seem to be a << , though (after a few minutes of Hoogling). Why is this? Edit: I know it's not a big deal. I just like the way certain lines of code look with the left-pointing operators. x <- doSomething =<< doSomethingElse just looks nicer, with the arrows all going the same way, than x <- doSomethingElse >>= doSomething . To the best

STM monad problem

谁说我不能喝 提交于 2019-11-29 06:00:47
This is just a hypothetical scenario to illustrate my question. Suppose that there are two threads and one TVar shared between them. In one thread there is an atomically block that reads the TVar and takes 10s to complete. In another thread is an atomically block that modifies the TVar every second. Will the first atomically block ever complete? Surely it will just keep going back to the beginning, because the log is perpetually in an inconsistent state? As others have said: in theory there is no guarantee of progress. In practice there is also no guarantee of progress: import Control.Monad --

Folding, function composition, monads, and laziness, oh my?

感情迁移 提交于 2019-11-29 05:53:37
问题 I am puzzled. I can write this: import Control.Monad main = print $ head $ (foldr (.) id [f, g]) [3] where f = (1:) g = undefined and the output is 1 . That makes sense, because it reduces to: main = print $ head $ ((1:) . undefined . id) [3] main = print $ head $ (1:) ((undefined . id) [3]) main = print $ head $ 1 : ((undefined . id) [3]) main = print $ 1 But if I use a vaguely similar monadic technique, it doesn't work the same: import Control.Monad main = print $ (foldr (<=<) return [f, g]

Why is ListT monad transformer considered buggy - what monad laws it breaks?

我的未来我决定 提交于 2019-11-29 05:28:48
I've seen mentioned that ListT is a classic example of a buggy monad transformer that doesn't satisfy the monad laws . Can this be demonstrated by a simple example? Edit: My idea with ListT [] is a bit wrong, I missed that the documentation requires the inner monad to be commutative. So, is ListT buggy just in the sense that has this requirement, or is there another problem? (The examples at Haskell wiki all use ListT IO and IO is obviously not commutative.) A simple example that shows how it fails the associativity law: v :: Int -> ListT [] Int v 0 = ListT [[0, 1]] v 1 = ListT [[0], [1]] main

How to take out a value out of a monad in Haskell?

孤街浪徒 提交于 2019-11-29 03:47:19
Is there any way to take "things" out of a monad? I am developing a game, and I am now trying to understand about databases. I found happstack really nice, but I can't get the thing. For example, I have this function (hope you are familiar with happstack ) getAllThings :: MonadIO m => m [Thing] getAllThings = do elems <- query GetThings return elems So I get m [Things] , but I can't use this in my model! For instance doSomeThingWithThings :: [Thing] -> Something I googled this and I found nothing. bravit You are not supposed to exit IO monad this way (except unsafePerformIO function), but you

Understanding Random monad in Scala

佐手、 提交于 2019-11-29 02:52:11
问题 This a follow-up to my previous question Travis Brown pointed out that java.util.Random is side-effecting and suggested a random monad Rng library to make the code purely functional. Now I am trying to build a simplified random monad by myself to understand how it works. Does it make sense ? How would you fix/improve the explanation below ? Random Generator First we plagiarize a random generating function from java.util.Random // do some bit magic to generate a new random "seed" from the