monads

How to interpret bind/>>= of the function instance?

痴心易碎 提交于 2019-11-27 04:49:53
问题 I'm trying to improve my understanding of Applicative s and Monad s by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all. Here are my implementations of fmap , <*> and >>= for the Functor , Applicative and Monad typeclasses in Javascript: const fmap = f => g => x => f(g(x)); // B combinator const apply = f => g => x => f(x) (g(x)); // S combinator const bind = f => g => x => g(f(x)) (x); // ? I am not sure

How are mutable arrays implemented in Haskell?

旧街凉风 提交于 2019-11-27 04:21:57
问题 I've read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the "type" Array itself should be defined, they only gave definitions for the functions using monads to access or modify this type. How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell ?! (such as STUArray and MArray) 回答1: How are arrays, having O(1) time to access or modify an indexed

In what sense is the IO Monad pure?

爱⌒轻易说出口 提交于 2019-11-27 04:11:31
问题 I've had the IO monad described to me as a State monad where the state is "the real world". The proponents of this approach to IO argue that this makes IO operations pure, as in referentially transparent. Why is that? From my perspective it appears that code inside the IO monad have plenty of observable side effects. Also, isn't it possible to describe pretty much any non-pure function like a function of the real world? For example, can't we think of, say, C's malloc as being a function that

Why are Promises Monads?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 04:05:29
I've been learning about functional programming and have come across Monads, Functors and Applicatives. From my understanding the following definitions apply: a) ( A=>B ) => C[A] => C[B] | Functor b) ( A=>C[B] ) => C[A] => C[B] | Monad c) ( C[A=>B] ) => C[A] => C[B] | Applicative (reference: https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/ ) Furthermore, I understand a Monad is a special case of a Functor. As in, it applies a function that returns a wrapped value to a wrapped value and returns a wrapped value. When we use Promise.then(func) , we are

Is Haskell truly pure (is any language that deals with input and output outside the system)?

こ雲淡風輕ζ 提交于 2019-11-27 03:21:10
After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard maths? EDIT: This is not flame bait as someone has said in this post, but a genuine question that I am hoping that someone can shoot me down with and say, proof, it is pure. Also I am looking at the question with respect to other not so pure Functional Languages and some OO languages that use good design and comparing the purity. So far in my very limited world

Difference between Monad and Applicative in Haskell

丶灬走出姿态 提交于 2019-11-27 02:50:42
I just read the following from typeclassopedia about the difference between Monad and Applicative . I can understand that there is no join in Applicative . But the following description looks vague to me and I couldn't figure out what exactly is meant by "the result" of a monadic computation/action. So, if I put a value into Maybe , which makes a monad, what is the result of this "computation"? Let’s look more closely at the type of (>>=). The basic intuition is that it combines two computations into one larger computation. The first argument, m a, is the first computation. However, it would

Eliminating my explicit state passing via like, monads and stuff

人走茶凉 提交于 2019-11-27 02:47:49
问题 I'm working through the book Land of Lisp in F# (yeah weird, I know). For their first example text adventure, they make use of global variable mutation and I'd like to avoid it. My monad-fu is weak, so right now I'm doing ugly state passing like this: let pickUp player thing (objects: Map<Location, Thing list>) = let objs = objects.[player.Location] let attempt = objs |> List.partition (fun o -> o.Name = thing) match attempt with | [], _ -> "You cannot get that.", player, objs | thing :: _,

How to use an Alex monadic lexer with Happy?

懵懂的女人 提交于 2019-11-27 02:45:30
问题 I'm trying to learn using Alex + Happy to build parser, in particular I'm interested in learning to use the monad wrapper of Alex. I have already looked at the documentation of Alex and Happy but I they are both, for me, really lacking any useful information on using them together. I managed to make them work together with the basic and posn wrappers, but I'm at a loss with monad . I have already looked at different question on SO about Alex, Happy and monadic lexers (including: Are there any

Restricting a monad to a type class

試著忘記壹切 提交于 2019-11-27 02:45:08
问题 In Haskell, is there a way to restrict a monad M a so that a satisfy a type class constraint? I am translating the probabilistic modeling example from F# to Haskell. However, in Haskell, I omitted support because it would change data Distribution a to data (Ord a) => Distribution a . With this change, I get the following error: ...probabilisticModeling.hs:42:13: Could not deduce (Ord a) from the context () arising from a use of `always' at ...probabilisticModeling.hs:42:13-18 Possible fix:

What is the purpose of the reader monad?

流过昼夜 提交于 2019-11-27 02:36:19
The reader monad is so complex and seems to be useless. In an imperative language like Java or C++, there is no equivalent concept for the reader monad, if I am not mistaken. Can you give me a simple example and clear this up a little bit? Philip JF Don't be scared! The reader monad is actually not so complicated, and has real easy-to-use utility. There are two ways of approaching a monad: we can ask What does the monad do ? What operations is it equipped with? What is it good for? How is the monad implemented? From where does it arise? From the first approach, the reader monad is some