monads

How does the ST monad work?

余生颓废 提交于 2019-11-27 10:29:14
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 state monad (due to the forall )? I'm just throwing out ideas and would really appreciate someone more

Expressing do block using only monadic bind syntax

心不动则不痛 提交于 2019-11-27 09:45:06
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 possible to simply execute the readFile computation twice. How can this example (if possible at all)

Haskell - depth for each node in binary tree using Reader monad

ぃ、小莉子 提交于 2019-11-27 09:44:55
I wrote the following code. It is working and using the Reader monad. Could you give me some hints about code style in Haskell ? Mainly, I mean monads -- I am newbie. import Control.Monad.Reader data Tree a = Node a (Tree a) (Tree a) | Empty renumberM :: Tree a -> Reader Int (Tree Int) renumberM (Node _ l r) = ask >>= (\x -> return (Node x (runReader (local (+1) (renumberM l)) x) (runReader (local (+1) (renumberM r)) x))) renumberM Empty = return Empty renumber'' :: Tree a -> Tree Int renumber'' t = runReader (renumberM t) 0 I want to show you that your idea is an instance of a more general

When would I want to use a Free Monad + Interpreter pattern?

假如想象 提交于 2019-11-27 09:40:49
问题 I'm working on a project that, amongst other things, involves a database access layer. Pretty normal, really. In a previous project, a collaborator encouraged me to use the Free Monads concept for a database layer and so I did. Now I'm trying to decide in my new project what I gain. In the previous project, I had an API that looked rather like this. saveDocument :: RawDocument -> DBAction () getDocuments :: DocumentFilter -> DBAction [RawDocument] getDocumentStats :: DBAction [(DocId,

Is Haskell's mapM not lazy?

筅森魡賤 提交于 2019-11-27 09:25:10
UPDATE: Okay this question becomes potentially very straightforward. q <- mapM return [1..] Why does this never return? Does mapM not lazily deal with infinite lists? The code below hangs. However, if I replace line A by line B, it doesn't hang anymore. Alternatively, if I preceed line A by a "splitRandom $", it also doesn't hang. Q1 is: Is mapM not lazy? Otherwise, why does replacing line A with line B "fix this" code? Q2 is: Why does preceeding line A with splitRandom "solve" the problem? import Control.Monad.Random import Control.Applicative f :: (RandomGen g) => Rand g (Double, [Double]) f

Monad in non-programming terms [duplicate]

一笑奈何 提交于 2019-11-27 09:19:50
问题 Possible Duplicate: What is a monad? How would you describe a monad in non-programming terms? Is there some concept/thing outside of programming (outside of all programming, not just FP) which could be said to act or be monad-like in a significant way? 回答1: Here's my current stab at it: Monads are bucket brigades : Each operation is a person standing in line; i.e. there's an unambiguous sequence in which the operations take place. Each person takes one bucket as input, takes stuff out of it,

Pattern to avoid nested try catch blocks?

岁酱吖の 提交于 2019-11-27 09:16:57
问题 Consider a situation where I have three (or more) ways of performing a calculation, each of which can fail with an exception. In order to attempt each calculation until we find one that succeeds, I have been doing the following: double val; try { val = calc1(); } catch (Calc1Exception e1) { try { val = calc2(); } catch (Calc2Exception e2) { try { val = calc3(); } catch (Calc3Exception e3) { throw new NoCalcsWorkedException(); } } } Is there any accepted pattern which achieves this in a nicer

What are the benefits of applicative parsing over monadic parsing?

自闭症网瘾萝莉.ら 提交于 2019-11-27 09:15:33
问题 There seems to be a consensus that you should use Parsec as an applicative rather than a monad. What are the benefits of applicative parsing over monadic parsing? style performance abstraction Is monadic parsing out? 回答1: The main difference between monadic and applicative parsing is in how sequential composition is handled. In the case of an applicative parser, we use (<*>) , whereas with a monad we use (>>=) . (<*>) :: Parser (a -> b) -> Parser a -> Parser b (>>=) :: Parser a -> (a ->

Monads with Java 8

橙三吉。 提交于 2019-11-27 09:01:02
问题 In the interests of helping to understand what a monad is, can someone provide an example using java ? Are they possible ? Lambda expressions are possible using java if you download the pre-release lambda compatible JDK8 from here http://jdk8.java.net/lambda/ An example of a lambda using this JDK is shown below, can someone provide a comparably simple monad ? public interface TransformService { int[] transform(List<Integer> inputs); } public static void main(String ars[]) { TransformService

Monads vs. Arrows

血红的双手。 提交于 2019-11-27 09:00:21
问题 I'm broadly familiar with the concepts of monads and arrows as used in functional programming. I also understand that they can be used to solve similar kinds of problems. However, I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? 回答1: There are two excellent papers by Lindley, Wadler & Yallop (discussed at LTU here). The most important thing to understand is that there are more things which are arrows