monads

How to apply higher order function to an effectful function in Haskell?

我与影子孤独终老i 提交于 2019-11-27 16:12:55
问题 I have too functions: higherOrderPure :: (a -> b) -> c effectful :: Monad m => (a -> m b) I'd like to apply the first function to the second: higherOrderPure `someOp` effectful :: Monad m => m c where someOp :: Monad m => ((a -> b) -> c) -> (a -> m b) -> m c Example: curve :: (Double -> Double) -> Dia Any curve f = fromVertices $ map p2 [(x, f x) | x <- [1..100]] func :: Double -> Either String Double func _ = Left "Parse error" -- in other cases this func can be a useful arithmetic

How to handle side effect with Applicative?

送分小仙女□ 提交于 2019-11-27 15:39:08
I see everywhere that Applicative can handle side effects, but all the simple examples I've seen are just combining stuff together like: > (,,) <$> [1,2] <*> ["a", "b", "c"] <*> ["foo", "bar"] [(1,"a","foo"),(1,"a","bar"),(1,"b","foo"),(1,"b","bar"), (1,"c","foo"),(1,"c","bar"),(2,"a","foo"),(2,"a","bar"), (2,"b","foo"),(2,"b","bar"),(2,"c","foo"),(2,"c","bar")] Which is cool but I can't see how that links to side effects. My understanding is that Applicative is a weak monad and so you can handle side effects (as you would do with a State monad) but you can't reuse the result of the previous

Unsequence Monad function within Haskell

你离开我真会死。 提交于 2019-11-27 15:34:14
I'm having some real trouble designing the counterfunction of Haskell's sequence function, which Hoogle tells me doesn't yet exist. This is how it behaves: ghci> sequence [Just 7, Just 8, Just 9] Just [7,8,9] ghci> sequence [getLine, getLine, getLine] hey there stack exchange ["hey","there","stack exchange"] :: IO [String] My problem is making a function like this: unsequence :: (Monad m) => m [a] -> [m a] So that it behaves like this: ghci> unsequence (Just [7, 8, 9]) [Just 7, Just 8, Just 9] ghci> sequence getLine hey ['h','e','y'] :: [IO Char] --(This would actually cause an error, but hey

Scalaz Bind[Seq] typeclass

馋奶兔 提交于 2019-11-27 15:17:45
I'm currently porting some code from traditional Scala to Scalaz style. It's fairly common through most of my code to use the Seq trait in my exposed API signatures rather than a concrete type (i.e. List, Vector) directly. However, this poses some problem with Scalaz, since it doesn't provide an implementation of a Bind[Seq] typeclass. i.e. This will work correctly. List(1,2,3,4) >>= bindOperation But this will not Seq(1,2,3,4) >>= bindOperation failing with the error could not find implicit value for parameter F0: scalaz.Bind[Seq] I assume this is an intentional design decision in Scalaz -

Why can applicative functors have side effects, but functors can't?

匆匆过客 提交于 2019-11-27 14:54:13
问题 I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...? 回答1: This answer is a bit of an over-simplification, but if we define side effects as computations being affected by previous computations, it's easy to see that the Functor typeclass is insufficient for side effects simply because there is no way

How can non-determinism be modeled with a List monad?

删除回忆录丶 提交于 2019-11-27 14:23:50
问题 Can anyone explain (better with an example in plain English) what a list monad can do to model non-deterministic calculations? Namely what the problem is and what solution a list monad can offer. 回答1: Here's an example based on coin tossing. The problem is as follows: You have two coins, labeled Biased and Fair . The Biased coin has two heads, and the Fair coin has one head and one tail. Pick one of these coins at random, toss it and observe the result. If the result is a head, what is the

What's so special about 'return' keyword

冷暖自知 提交于 2019-11-27 13:23:27
问题 When I seemed to understand what return is for in Haskell, I tried to play with different alternatives and it seems that return not only can be used anywhere in the monad chain, but also can be excluded completely *Main> Just 9 >>= \y -> (Just y) >>= \x -> return x Just 9 *Main> Just 9 >>= \y -> (return y) >>= \x -> (Just y) Just 9 *Main> Just 9 >>= \y -> (Just y) >>= \x -> (Just x) Just 9 Even if I omit return in my own instancing, I only get warning... data MaybeG a = NothingG | JustG a

Does Haskell have variables?

风流意气都作罢 提交于 2019-11-27 12:39:42
I've frequently heard claims that Haskell doesn't have variables; in particular, this answer claims that it doesn't, and it was upvoted at least nine times and accepted. So does it have variables or not, and why? This question also appears to apply ML, F#, OCaml, Erlang, Oz, Lava, and all SSA intermediate languages. Don Stewart Haskell has immutable variables (variables in the math sense) by default: foo x y = x + y * 2 By default variables are not mutable cells . Haskell also has mutable cells though, but you enable them explicitly: > import Data.IORef (newIORef, readIORef, writeIORef) > v <-

Is there a monad that doesn't have a corresponding monad transformer (except IO)?

冷暖自知 提交于 2019-11-27 11:25:07
So far, every monad (that can be represented as a data type) that I have encountered had a corresponding monad transformer, or could have one. Is there such a monad that can't have one? Or do all monads have a corresponding transformer? By a transformer t corresponding to monad m I mean that t Identity is isomorphic to m . And of course that it satisfies the monad transformer laws and that t n is a monad for any monad n . I'd like to see either a proof (ideally a constructive one) that every monad has one, or an example of a particular monad that doesn't have one (with a proof). I'm interested

How can I parse the IO String in Haskell?

空扰寡人 提交于 2019-11-27 10:33:40
问题 I' ve got a problem with Haskell. I have text file looking like this: 5. 7. [(1,2,3),(4,5,6),(7,8,9),(10,11,12)]. I haven't any idea how can I get the first 2 numbers (2 and 7 above) and the list from the last line. There are dots on the end of each line. I tried to build a parser, but function called 'readFile' return the Monad called IO String. I don't know how can I get information from that type of string. I prefer work on a array of chars. Maybe there is a function which can convert from