monads

MonadFix instance for interpreter monad transformer generated by FreeT?

混江龙づ霸主 提交于 2019-12-04 11:36:56
问题 I have a simplified version of the standard interpreter monad transformer generated by FreeT : data InteractiveF p r a = Interact p (r -> a) type Interactive p r = FreeT (InteractiveF p r) p is the "prompt", and r is the "environment"...one would run this using something like: runInteractive :: Monad m => (p -> m r) -> Interactive p r m a -> m a runInteractive prompt iact = do ran <- runFreeT iact case ran of Pure x -> return x Free (Interact p f) -> do response <- prompt p runInteractive

How do I handle an infinite list of IO objects in Haskell?

拈花ヽ惹草 提交于 2019-12-04 11:18:20
问题 I'm writing a program that reads from a list of files. The each file either contains a link to the next file or marks that it's the end of the chain. Being new to Haskell, it seemed like the idiomatic way to handle this is is a lazy list of possible files to this end, I have getFirstFile :: String -> DataFile getNextFile :: Maybe DataFile -> Maybe DataFile loadFiles :: String -> [Maybe DataFile] loadFiles = iterate getNextFile . Just . getFirstFile getFiles :: String -> [DataFile] getFiles =

What is Crockford's law?

拈花ヽ惹草 提交于 2019-12-04 10:17:39
问题 Someone referenced "Crockford's law" recently with respect to monads. Google shows very little in the way of results. Anyone know what it is? 回答1: Assuming "Crockford's Law" is The Curse that he mentions early in the video, he's referring to this common occurrence (described much more eloquently here): person X doesn't understand monads person X works long and hard, and groks monads person X experiences amazing feeling of enlightenment, wonders why others are not similarly enlightened person

Translate from monad to applicative

喜欢而已 提交于 2019-12-04 10:03:22
问题 OK, so I know what the Applicative type class contains, and why that's useful. But I can't quite wrap my brain around how you'd use it in a non-trivial example. Consider, for example, the following fairly simple Parsec parser: integer :: Parser Integer integer = do many1 space ds <- many1 digit return $ read ds Now how the heck would you write that without using the Monad instance for Parser ? Lots of people claim that this can be done and is a good idea, but I can't figure out how exactly.

Is it possible to do the IO monad from Haskell in Clojure?

孤人 提交于 2019-12-04 09:26:44
问题 I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't appear to 'implement the monad interface' (for want of a better phrasing) This is example using ST in Haskell oneST :: ST s Int -- note that this works correctly for any s oneST = do var <- newSTRef 0 modifySTRef var (+1) readSTRef var one :: Int one =

Confusion over IORefs to make a counter

半腔热情 提交于 2019-12-04 09:01:59
I found some sample code, and changed it a little counter = unsafePerform $ newIORef 0 newNode _ = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i Which returns 1 then 2 then 3 then 3 etc each time it's run. But when I change it to newNode = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i then I get 0 every time I run it. Why is this happening, and what can I do to fix it? In your second version newNode is a simple value, not a function. So haskell evaluates it exactly once and then gives you the result of that evaluation whenever you

How do I make a do block return early?

不打扰是莪最后的温柔 提交于 2019-12-04 09:01:47
问题 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)

Are all differentiable types Monads

£可爱£侵袭症+ 提交于 2019-12-04 08:59:22
问题 Given a differentiable type, we know that its Zipper is a Comonad. In response to this, Dan Burton asked, "If derivation makes a comonad, does that mean that integration makes a monad? Or is that nonsense?". I'd like to give this question a specific meaning. If a type is differentiable, is it necessarily a monad? One formulation of the question would be to ask, given the following definitions data Zipper t a = Zipper { diff :: D t a, here :: a } deriving instance Diff t => Functor (Zipper t)

Should I avoid using Monad fail?

大城市里の小女人 提交于 2019-12-04 08:49:43
问题 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

Map and Reduce Monad for Clojure… What about a Juxt Monad?

我的未来我决定 提交于 2019-12-04 08:11:13
问题 Whilst learning Clojure, I've spent ages trying to make sense of monads - what they are and how we can use them.... with not too much success. However, I found an excellent 'Monads for Dummies' Video Series - http://vimeo.com/20717301 - by Brian Marik for Clojure So far, my understanding of monads is that it is sort of like a macro in that it allows a set of statements to be written in a form that is easy to read - but monads are much more formalised. My observations are limited to two