monads

Interpreting Haskell monad example

天涯浪子 提交于 2019-12-14 00:00:25
问题 I have this Haskell code that triples input value. triple :: Int -> Int triple = do n <- id d <- (n+) (d+) How does this code work? With an example triple 10 , how the argument 10 is mapped/assigned to id , n and d to get the return value of 30? My understandig is as follows: We can decompose triple function with two subfunctions tripleA and tripleB as follows: triple :: Int -> Int triple = tripleA >>= (\d -> tripleB d) tripleA :: Int -> Int tripleA = id >>= (\n -> (n+)) tripleB :: Int -> Int

Pattern matching in `Alternative`

China☆狼群 提交于 2019-12-13 16:49:03
问题 I have a function that pattern matches on its arguments to produce a computation in StateT () Maybe () . This computation can fail when run, in which case I want the current pattern match branch to fail, so to speak. I highly doubt it's possible to have something like compute :: Int -> StateT () Maybe Int compute = return f :: Maybe Int -> Maybe Int -> StateT () Maybe () f (Just n1) (Just n2) = do m <- compute (n1 + n2) guard (m == 42) f (Just n) _ = do m <- compute n guard (m == 42) f _

Why does smallCheck's `Series` class have two types in the constructor?

梦想与她 提交于 2019-12-13 15:14:15
问题 This question is related to my other question about smallCheck 's Test.SmallCheck.Series class. When I try to define an instance of the class Serial in the following natural way (suggested to me by an answer by @tel to the above question), I get compiler errors: data Person = SnowWhite | Dwarf Int instance Serial Person where ... It turns out that Serial wants to have two arguments. This, in turn, necessitates a some compiler flags. The following works: {-# LANGUAGE FlexibleInstances,

How to preserve information when failing?

a 夏天 提交于 2019-12-13 14:19:00
问题 I'm writing some code that uses the StateT monad transformer to keep track of some stateful information (logging and more). The monad I'm passing to StateT is very simple: data CheckerError a = Bad {errorMessage :: Log} | Good a deriving (Eq, Show) instance Monad CheckerError where return x = Good x fail msg = Bad msg (Bad msg) >>= f = Bad msg (Good x) >>= f = f x type CheckerMonad a = StateT CheckerState CheckerError a It's just a Left and Right variant. What troubles me is the definition of

Random-Pivot Quicksort in Haskell

我们两清 提交于 2019-12-13 13:32:56
问题 Is it possible to implement a quicksort in Haskell (with RANDOM-PIVOT) that still has a simple Ord a => [a]->[a] signature? I'm starting to understand Monads, and, for now, I'm kind of interpreting monads as somethink like a 'command pattern', which works great for IO. So, I understand that a function that returns a random number should actually return a monadic value like IO, because, otherwise, it would break referential transparency. I also understand that there should be no way to

Reader monad in Scala: return, local, and sequence

a 夏天 提交于 2019-12-13 13:27:03
问题 I'm using the Reader monad in Scala as provided by the scalaz library. I'm familiar with this monad as defined in Haskell. The problem is that I cannot find the functions equivalent to return , local , and sequence (among others). Currently I use constructs that I do not like since I'm repeating myself or making my code a bit obscure. Regarding return , I'm currently using: Reader{_ => someValue} I'd rather just use a construct like unit(someValue) , but I could not find anything on the

Why wrap an IO result in IO Monad

馋奶兔 提交于 2019-12-13 12:41:18
问题 In Haskell we have a function readFile :: FilePath -> IO String . My question while understanding monad is why wrap it in IO ? Couldn't we just have written function like these: (lines.readFile) path Rather than (readFile >>= lines) path What benefit does the IO wrapper provide? 回答1: Haskell expressions are referentially transparent. This means that if readFile would really have a type of FilePath -> String , then expression readFile "a.txt" would always yield the same result. Even if you

Why is flatMap on a Vector[Option[Int]] whose mapper function result is not a Vector[Option[Int]] valid?

Deadly 提交于 2019-12-13 12:10:24
问题 For example, Vector(Some(1), Some(2), Some(3), None).flatMap{ n => n } produces a Vector(1, 2, 3) instead of giving an error. As I have seen in other languages, flatMap is used when you have a mapper function that produces nesting so I would expect this to be a valid flatMap : Vector(1, 2, 3).flatMap{ eachNum => Vector(eachNum) } My mapper function produces a Vector which would cause nesting (i.e. Vector(Vector(1), Vector(2), Vector(3), Vector(4)) ) if I used a map due to the container

How to implement index-core style indexed continuation monad

前提是你 提交于 2019-12-13 11:43:36
问题 I have been looking at (and trying to understand) indexed monads recently. I think I have got my head around one style of indexed monad, as described here: A Neighbourhood of Infinity: Beyond Monads. However, I have found a different style of indexed monad in index-core, which has some parts that seem to correspond to this indexed monad bind with two indexes, for example a similar bind operator !>=. While it clearly has similar changes to the indexes, I can't quite understand how to use these

Monads not with “flatMap” but “flatUnit”? [closed]

左心房为你撑大大i 提交于 2019-12-13 11:32:00
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . Monads in category theory is defined by triples T, unit, flat ⟩. class Monad t where map :: (a -> b) -> (t a -> t b) -- functorial action unit :: a -> t a flat :: t (t a) -> t a class KleisliTriple t where unit :: a -> t a flatMap :: t a -> (a -> t b) -> t b KleisliTriple flats the structure by the