monads

How can I parse the IO String in Haskell?

人走茶凉 提交于 2019-11-28 17:30:46
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 'IO String' to [Char]? I think you have a fundamental misunderstanding about IO in Haskell.

Is 'Chaining operations' the “only” thing that the Monad class solves?

只谈情不闲聊 提交于 2019-11-28 17:29:09
问题 To clarify the question: it is about the merits of the monad type class (as opposed to just its instances without the unifying class). After having read many references (see below), I came to the conclusion that, actually, the monad class is there to solve only one, but big and crucial , problem: the 'chaining' of functions on types with context . Hence, the famous sentence "monads are programmable semicolons". In fact, a monad can be viewed as an array of functions with helper operations. I

What exactly makes Option a monad in Scala?

爱⌒轻易说出口 提交于 2019-11-28 17:25:56
问题 I know what the monads are and how to use them. What I don't understand is what makes, let's say, Option a monad? In Haskell a monad Maybe is a monad because it's instantiated from Monad class (which has at least 2 necessary functions return and bind that makes class Monad , indeed, a monad). But in Scala we've got this: sealed abstract class Option[+A] extends Product with Serializable { ... } trait Product extends Any with Equals { ... } Nothing related to a monad. If I create my own class

Haskell: can't use “map putStrLn”?

左心房为你撑大大i 提交于 2019-11-28 16:58:38
问题 I have a list of strings, and tried this: ls = [ "banana", "mango", "orange" ] main = do map PutStrLn list_of_strings That didn't work, and I can't understand why. ghc print-list.hs print-list.hs:3:0: Couldn't match expected type `IO t' against inferred type `[IO ()]' In the expression: main When checking the type of the function `main' Any hints? I suppose it has to do with map returning a list and not a value, but I didn't find an easy way to fix this. Right now the only way I know to print

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

我怕爱的太早我们不能终老 提交于 2019-11-28 16:21:34
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, DocumentStats)] etc. About twenty such public functions. To support them, I had the DBAction data structure:

ST Monad == code smell?

淺唱寂寞╮ 提交于 2019-11-28 16:11:54
I'm working on implementing the UCT algorithm in Haskell, which requires a fair amount of data juggling. Without getting into too much detail, it's a simulation algorithm where, at each "step," a leaf node in the search tree is selected based on some statistical properties, a new child node is constructed at that leaf, and the stats corresponding to the new leaf and all of its ancestors are updated. Given all that juggling, I'm not really sharp enough to figure out how to make the whole search tree a nice immutable data structure à la Okasaki . Instead, I've been playing around with the ST

Monad in non-programming terms [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 15:40:20
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? 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, and puts new stuff in the bucket. The bucket, in turn, is passed down to the next person in the brigade

Pattern to avoid nested try catch blocks?

情到浓时终转凉″ 提交于 2019-11-28 15:33:24
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 way? Of course I could wrap each calculation in a helper method which returns null on failure, and then

What are the benefits of applicative parsing over monadic parsing?

老子叫甜甜 提交于 2019-11-28 15:30:59
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? 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 -> Parser b) -> Parser b The monadic approach is more flexible, because it allows the grammar of the second part to

Is Future in Scala a monad?

给你一囗甜甜゛ 提交于 2019-11-28 15:25:40
Why and how specifically is a Scala Future not a Monad; and would someone please compare it to something that is a Monad, like an Option? The reason I'm asking is Daniel Westheide's The Neophyte's Guide to Scala Part 8: Welcome to the Future where I asked whether or not a Scala Future was a Monad, and the author responded that it wasn't, which threw off base. I came here to ask for a clarification. A summary first Futures can be considered monads if you never construct them with effectful blocks (pure, in-memory computation), or if any effects generated are not considered as part of semantic