monads

How Monads are considered Pure?

你。 提交于 2019-12-09 12:22:32
问题 I am very much new to Haskell, and really impressed by the language's "architecture", but it still bothers me how monads can be pure. As you have any sequence of instructions, it makes it an impure function, especially functions with I/O wouldn't be pure from any point of view. Is it because Haskell assumes, like all pure functions, that IO function has a return value too, but in form of opcode or something? I am really confused. 回答1: One way to think of this is that a value of type IO a is a

Haskell — “The last statement in a 'do' construct must be an expression”

怎甘沉沦 提交于 2019-12-09 08:14:14
问题 Like it says in the title: What does The last statement in a 'do' construct must be an expression mean? I ended my do block with a putStrLn like it shows in several examples I've seen, and i get an error. Code: main = do args <- getArgs file <-readFile "TWL06.txt" putStrLn results 回答1: Most of the time, it's because your code is mis-aligned and compiler assumes that your "do" block ended prematurely (or has extra code that dont really belong there) 回答2: Your last line isn't something like

How are Scala Futures chained together with flatMap?

烂漫一生 提交于 2019-12-09 06:02:52
问题 I'm working on using Futures for the first time in Scala and am working through an example of using the flatMap combinator; I've been following this discussion: http://docs.scala-lang.org/overviews/core/futures.html Specifically, this example: val usdQuote = future { connection.getCurrentValue(USD) } val chfQuote = future { connection.getCurrentValue(CHF) } val purchase = for { usd <- usdQuote chf <- chfQuote if isProfitable(usd, chf) } yield connection.buy(amount, chf) purchase onSuccess {

(How) can I make this monadic bind tail-recursive?

会有一股神秘感。 提交于 2019-12-09 05:54:50
问题 I have this monad called Desync - [<AutoOpen>] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to figure out how to /// make Desync implementation tail-recursive (see note about unbounded recursion in bind). And /// frankly, I'm not sure if there is a tail-recursive implementation of it... type [<NoComparison; NoEquality>] Desync<'e, 's, 'a> = Desync of (

Under what circumstances are monadic computations tail-recursive?

独自空忆成欢 提交于 2019-12-09 05:02:46
问题 In Haskell Wiki's Recursion in a monad there is an example that is claimed to be tail-recursive: f 0 acc = return (reverse acc) f n acc = do v <- getLine f (n-1) (v : acc) While the imperative notation leads us to believe that it is tail-recursive, it's not so obvious at all (at least to me). If we de-sugar do we get f 0 acc = return (reverse acc) f n acc = getLine >>= \v -> f (n-1) (v : acc) and rewriting the second line leads to f n acc = (>>=) getLine (\v -> f (n-1) (v : acc)) So we see

Side effects in Scala

喜夏-厌秋 提交于 2019-12-09 02:29:40
问题 I am learning Scala right in these days. I have a slight familiarity with Haskell, although I cannot claim to know it well. Parenthetical remark for those who are not familiar with Haskell One trait that I like in Haskell is that not only functions are first-class citizens, but side effects (let me call them actions) are. An action that, when executed, will endow you with a value of type a , belongs to a specific type IO a . You can pass these actions around pretty much like any other value,

Is this a valid monad transformer in Javascript?

左心房为你撑大大i 提交于 2019-12-09 00:13:14
问题 In order to better understand monad transformers I implemented one. Since Javascript is dynamically typed I don't mimic type or data constructors but declare only plain old Javascript objects, which hold the corresponding static functions to form a specific monad / transformer. The underlying idea is to apply these methods to a value/values in a container type. Types and containers are separated so to speak. Array s can contain any number of elements. It is trivial to extend Array s so that

bind can be composed of fmap and join, so do we have to use monadic functions a -> m b?

隐身守侯 提交于 2019-12-08 19:12:19
问题 I don't use Haskell a lot, but I understand the concept of Monads. I had been confused by Kleisli triple, and the category, however, fmap and join Although Haskell defines monads in terms of the return and bind functions, it is also possible to define a monad in terms of return and two other operations, join and fmap . This formulation fits more closely with the original definition of monads in category theory. The fmap operation, with type (t → u) → M t → M u , takes a function between two

Is there a principled way to compose two monad transformers if they are of different type, but their underlying monad is of the same type?

只愿长相守 提交于 2019-12-08 17:43:49
问题 Not much I can do to expand the question. But here is a use case: let's say you have two monad transformers, t and s , transforming over the same monad m : master :: (MonadTrans t, Monad m) => t m a b slave :: (MonadTrans t, Monad m) => s m a b And I want to compose master and slave such that they can communicate with each other when m primitives are lifted into t and s . The signature might be: bound :: (MonadTrans t, MonadTrans s, Monad m, Monoid a) => t m a b -> s m a b -> (...) But what

Proving equivalence of sequence definitions from Applicative and Monad

六眼飞鱼酱① 提交于 2019-12-08 17:43:30
问题 How can I properly prove that sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) sequenceA [] = pure [] sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs is essentially the same to monad inputs as sequenceA' :: Monad m => [m a] -> m [a] sequenceA' [] = return [] sequenceA' (x:xs) = do x' <- x xs' <- sequenceA' xs return (x':xs') In spite of the constraint Applicative and Monad of course. 回答1: Here's a proof sketch: Show that do x' <- x xs' <- sequenceA' xs return (x' : xs') is