monads

When is it OK to use an IORef?

泄露秘密 提交于 2019-12-03 02:08:22
问题 One thing that has always confused me is whether or not it's an okay time to use an IORef. Are there any guidelines that should be followed when deciding whether or not to use an IORef for a task? When is a good time to use the State monad over an IORef? 回答1: State and its relative ST both produce `monolithic' stateful computations which may be run as units. They basically treat the mutable state as intermediate data, which is needed to produce a result, but should not, in and of itself, be

Avoiding lift with monad transformers

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:53:51
问题 I have a problem to which a stack of monad transformers (or even one monad transformer) over IO . Everything is good, except that using lift before every action is terribly annoying! I suspect there is really nothing to do about that, but I thought I'd ask anyway. I am aware of lifting entire blocks, but what if the code is really of mixed types? Would it not be nice if GHC threw in some syntactic sugar (for example, <-$ = <- lift )? 回答1: For all the standard mtl monads, you don't need lift

How do I make a do block return early?

我的梦境 提交于 2019-12-03 01:51:42
I'm trying to scrape for a webpage using Haskell and compile the results into an object. If, for whatever reason, I can't get all the items from the pages, I want to stop trying to process the page and return early. For example: scrapePage :: String -> IO () scrapePage url = do doc <- fromUrl url title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText when (isNothing title) (return ()) date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc" when (isNothing date) (return ()) -- etc -- make page object and send it to db return () The problem is the when doesn't stop

Why can the Monad interface not be declared in Java?

房东的猫 提交于 2019-12-03 01:39:41
问题 Before you start reading: This question is not about understanding monads, but it is about identifying the limitations of the Java type system which prevents the declaration of a Monad interface. In my effort to understand monads I read this SO-answer by Eric Lippert on a question which asks about a simple explanation of monads. There, he also lists the operations which can be executed on a monad: That there is a way to take a value of an unamplified type and turn it into a value of the

Where can I learn advanced Haskell? [closed]

做~自己de王妃 提交于 2019-12-03 01:31:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . In a comment to one of my answers, SO user sdcwc essentially pointed out that the following code: comb 0 = [[]] comb n = let rest = comb (n-1) in map ('0':) rest ++ map ('1':) rest could be replaced by: comb n = replicateM n "01" which had me completely stunned. Now I am looking for a tutorial, book or PDF that

Is operational really isomorphic to a free monad?

一曲冷凌霜 提交于 2019-12-03 01:30:53
问题 Proofs In this blog post, Tekmo makes the point that we can prove that ExitSuccess exits because (I presume) it's like the Const functor for that constructor (it doesn't carry the x so fmap behaves like const ). With the operational package, Tekmo's TeletypeF might be translated something like this: data TeletypeI a where PutStrLn :: String -> TeletypeI () GetLine :: TeletypeI String ExitSuccess :: TeletypeI () I've read that operational is isomorphic to a free monad, but can we prove here

What is a monad in FP, in categorical terms?

不羁的心 提交于 2019-12-03 01:29:07
问题 Every time someone promises to "explain monads", my interest is piqued, only to be replaced by frustration when the alleged "explanation" is a long list of examples terminated by some off-hand remark that the "mathematical theory" behind the "esoteric ideas" is "too complicated to explain at this point". Now I'm asking for the opposite. I have a solid grasp on category theory and I'm not afraid of diagram chasing, Yoneda's lemma or derived functors (and indeed on monads and adjunctions in the

Avoiding IORefs in pure code

僤鯓⒐⒋嵵緔 提交于 2019-12-03 01:18:29
I noticed that Data.UnionFind uses the IO monad to provide pointers via IORefs. I imagine everyone happily calls unsafePerformIO when using it locally in pure code, since the data structure is so well understood, but .. Is there a canonical cleaner approach to such data structures? Perhaps a wrapper around IO that makes the inevitable unsafePerformIO less unsafe "looking" by prohibiting most IO operations? Don Stewart Is there a canonical cleaner approach to such data structures? Perhaps a wrapper around IO that makes the inevitable unsafePerformIO less unsafe "looking" by prohibiting most IO

How does C# async/await relates to more general constructs, e.g. F# workflows or monads?

♀尐吖头ヾ 提交于 2019-12-03 01:10:30
问题 The C# language design have always (historically) been geared towards solving specific problems rather then finding to address the underlying general problems: see for example http://blogs.msdn.com/b/ericlippert/archive/2009/07/09/iterator-blocks-part-one.aspx for "IEnumerable vs. coroutines": We could have made it much more general. Our iterator blocks can be seen as a weak kind of coroutine. We could have chosen to implement full coroutines and just made iterator blocks a special case of

Should I avoid using Monad fail?

我只是一个虾纸丫 提交于 2019-12-03 01:03:33
I'm fairly new to Haskell and have been slowly getting the idea that there's something wrong with the existence of Monad fail. Real World Haskell warns against its use ("Once again, we recommend that you almost always avoid using fail!"). I just noticed today that Ross Paterson called it "a wart, not a design pattern" back in 2008 (and seemed to get quite some agreement in that thread). While watching Dr Ralf Lämmel talk on the essence of functional programming , I started to understand a possible tension which may have led to Monad fail. In the lecture, Ralf talks about adding various monadic