monads

How to handle side effect with Applicative?

一曲冷凌霜 提交于 2019-11-26 17:18:48
问题 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

Unsequence Monad function within Haskell

蹲街弑〆低调 提交于 2019-11-26 17:15:43
问题 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

Scalaz Bind[Seq] typeclass

情到浓时终转凉″ 提交于 2019-11-26 17:10:06
问题 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

A monad is just a monoid in the category of endofunctors, what's the problem?

谁都会走 提交于 2019-11-26 16:50:14
Who first said the following? A monad is just a monoid in the category of endofunctors, what's the problem? And on a less important note, is this true and if so could you give an explanation (hopefully one that can be understood by someone who doesn't have much Haskell experience)? Tom Crockett That particular phrasing is by James Iry, from his highly entertaining Brief, Incomplete and Mostly Wrong History of Programming Languages , in which he fictionally attributes it to Philip Wadler. The original quote is from Saunders Mac Lane in Categories for the Working Mathematician , one of the

How to use (->) instances of Monad and confusion about (->)

人走茶凉 提交于 2019-11-26 16:13:51
At different questions I've found hints in comments concerning using the (->) instance of Monads e.g. for realizing point-free style. As for me, this is a little too abstract. Ok, I've seen Arrow instances on (->) and it seems to me, that (->) can be used in instance notations but not in type declarations (that would alone be stuff for another question). Has anyone examples using (->) as instance of Monad? Or a good link? Sorry if this question may already have been discussed here, but searching for " (->) Monad instance" gives you many many hits as you can imagine ... since nearly every

Does Haskell have variables?

风流意气都作罢 提交于 2019-11-26 16:05:41
问题 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. 回答1: 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

How does the ST monad work?

北战南征 提交于 2019-11-26 15:12:53
问题 I understand that the ST monad is something like a little brother of IO, which in turn is the state monad with added RealWorld magic. I can picture states and I can picture that RealWorld is somehow put into IO, but every time I write a type signature of ST the s of the ST monad confuses me. Take, for example, ST s (STArray s a b) . How does the s work there? Is it just used to build up some artificial data dependency between computations without being able to be referenced like states in the

What is the point of the class Option[T]?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 15:05:55
I am not able to understand the point of Option[T] class in Scala. I mean, I am not able to see any advanages of None over null . For example, consider the code: object Main{ class Person(name: String, var age: int){ def display = println(name+" "+age) } def getPerson1: Person = { // returns a Person instance or null } def getPerson2: Option[Person] = { // returns either Some[Person] or None } def main(argv: Array[String]): Unit = { val p = getPerson1 if (p!=null) p.display getPerson2 match{ case Some(person) => person.display case None => /* Do nothing */ } } } Now suppose, the method

Expressing do block using only monadic bind syntax

主宰稳场 提交于 2019-11-26 14:53:17
问题 As far as I know, do blocks in Haskell are just some kind of syntactic sugar for monadic bind operators. For example, one could convert main = do f <- readFile "foo.txt" print f print "Finished" to main = readFile "foo.txt" >>= print >> print "Finished" Can all do blocks be converted to bind syntax? What about, for example, this block where f is used multiple times: main = do f <- readFile "foo.txt" print $ "prefix " ++ f print $ f ++ " postfix" Assuming we are in the IO monad, it is not

Monads with Join() instead of Bind()

霸气de小男生 提交于 2019-11-26 14:16:26
Monads are usually explained in turns of return and bind . However, I gather you can also implement bind in terms of join (and fmap ?) In programming languages lacking first-class functions, bind is excruciatingly awkward to use. join , on the other hand, looks quite easy. I'm not completely sure I understand how join works, however. Obviously, it has the [Haskell] type join :: Monad m => m (m x) -> m x For the list monad, this is trivially and obviously concat . But for a general monad, what, operationally, does this method actually do? I see what it does to the type signatures, but I'm