monads

Why does application of `sequence` on List of Lists lead to computation of its Cartesian Product?

笑着哭i 提交于 2019-11-30 12:32:53
问题 My question is about the sequence function in Prelude , the signature of which is as follows: sequence :: Monad m => [m a] -> m [a] I understand how this function works for List of Maybe s. For example, applying sequence on [Just 3, Just 9] gives Just [3, 9] . I noticed that applying sequence on List of List s gives its Cartesian Product. Can someone please help me understand how/why this happens? 回答1: This works because using lists as monads in Haskell makes them model indeterminism.

State Monad, sequences of random numbers and monadic code

走远了吗. 提交于 2019-11-30 12:03:02
问题 I'm trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Generator (probably not good, but my intention is just to learn the State Monad, not build a good RNG library). The generator is just this (I want to generate a sequence of Bool s for simplicity): type Seed = Int random :: Seed -> (Bool, Seed) random seed = let (a, c, m) = (1664525, 1013904223, 2^32) -- some params for the LCG

Can I make a Lens with a Monad constraint?

妖精的绣舞 提交于 2019-11-30 11:45:31
Context: This question is specifically in reference to Control.Lens (version 3.9.1 at the time of this writing) I've been using the lens library and it is very nice to be able to read and write to a piece (or pieces for traversals) of a structure. I then had a though about whether a lens could be used against an external database. Of course, I would then need to execute in the IO Monad . So to generalize: Question: Given a getter, (s -> m a) and an setter (b -> s -> m t) where m is a Monad, is possible to construct Lens s t a b where the Functor of the lens is now contained to also be a Monad?

How to use SmallCheck in Haskell?

狂风中的少年 提交于 2019-11-30 11:22:40
I am trying to use SmallCheck to test a Haskell program, but I cannot understand how to use the library to test my own data types. Apparently, I need to use the Test.SmallCheck.Series . However, I find the documentation for it extremely confusing. I am interested in both cookbook-style solutions and an understandable explanation of the logical (monadic?) structure. Here are some questions I have (all related): If I have a data type data Person = SnowWhite | Dwarf Integer , how do I explain to smallCheck that the valid values are Dwarf 1 through Dwarf 7 (or SnowWhite )? What if I have a

Is jQuery a monad

本秂侑毒 提交于 2019-11-30 11:19:59
问题 I read somewhere that jQuery is a monad and this answer shows that chain function in underscore.js library is not a monad (but comonad). And answer to this which is similar, shows that is monoid. So, is jQuery a monad? 回答1: Most APIs do not satisify the monad laws. jQuery is a large API , so statistically, it is unlikely to be "accidentally" monadic. As a result I am pretty skeptical that the jQuery API as a whole could satisfy the monad laws (i.e. that "jQuery is a monad"). This doesn't mean

Is there no standard (Either a) monad instance?

守給你的承諾、 提交于 2019-11-30 11:19:22
I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown module Main where import Control.Monad import Data.Either import Control.Monad.Instances test :: [Either a b] -> Either a [b] test = sequence main = return () but ghc tells me that it could not deduce (Monad (Either a)). Adding instance Monad (Either a) where return = Right Right b >>= f = f b Left a >>= _ = Left a makes the code compile, but this instance declaration seems so general that it doesn't

Can a `ST`-like monad be executed purely (without the `ST` library)?

一世执手 提交于 2019-11-30 11:12:20
问题 This post is literate Haskell. Just put in a file like "pad.lhs" and ghci will be able to run it. > {-# LANGUAGE GADTs, Rank2Types #-} > import Control.Monad > import Control.Monad.ST > import Data.STRef Okay, so I was able to figure how to represent the ST monad in pure code. First we start with our reference type. Its specific value is not really important. The most important thing is that PT s a should not be isomorphic to any other type forall s . (In particular, it should be isomorphic

Delimiting the IO monad

别等时光非礼了梦想. 提交于 2019-11-30 11:06:40
It's nice to know (in Safe Haskell, at least) from the signature whether or not something performs IO actions, but IO encompasses a lot of different things - putStr , database access, removing and writing to files, IORefs, etc. If I'm using the type signatures as a security measure when running arbitrary code, it might be the case that I'm willing to accept some IO actions - putStr and the ilk, for instance - but not others. Is there a way to define a restricted version of the IO monad, with only a subset of the normal IO actions? If so, an example (with putStr , for instance) would be very

Why there needs to be a $ in calls like “runSomeMonad $ do …”?

心已入冬 提交于 2019-11-30 10:48:54
Apparently the only possible interpretation of runSomeMonad do ... is runSomeMonad (do ...) . Why isn't the first variant allowed by the Haskell syntax? Is there some case where foo do bar could be actually ambiguous? Jon Purdy Note that you can observe this effect with not just do , but also let , if , \ , case , the extensions mdo and proc …and the dread unary - . I cannot think of a case in which this is ambiguous except for unary - . Here’s how the grammar is defined in the Haskell 2010 Language Report, §3: Expressions . exp → infixexp :: [context =>] type | infixexp infixexp → lexp qop

How to compose functions that return Option[List] in Scala?

强颜欢笑 提交于 2019-11-30 10:48:04
Suppose I have two functions to get orders and order items: def getOrders(): Option[List[Int]] = ... def getOrderItems(orderId: Int): Option[List[Int]] = ... Note that both functions return Option[List] since each function may fail. Now I would like to get Option of List of all order items as follows: return Some[List] if both functions return Some and None if any of them returns None. I tried to compose these functions with for (see below) but it did not work. val allOrderItems = for { orderIds <- getOrders(); orderId <- orderIds; orderItems <- getOrderItems(orderId) } yield orderItems How