monads

Why does this simple use of the State monad cause a stack overflow?

自作多情 提交于 2019-12-03 08:50:43
问题 I was playing around with the State monad, and I don't know what's causing the stack overflow in this simple piece of code. import Control.Monad.State.Lazy tick :: State Int Int tick = do n <- get put $! (n+1) return n million :: Int million = snd $ runState (mapM_ (const tick) [1..1000000]) 0 main = print million Note I would just like to know what's causing the problem in this piece of code, the task itself is not important per se. 回答1: The problem is that Control.Monad.State.Lazy's (>>=)

the equivalence between applicative functor and monad

这一生的挚爱 提交于 2019-12-03 08:47:13
People say monads are an extension of applicative functors, but I don't see that. Let's take an example of applicative functor: (<*>) :: f(a->b) -> f a -> f b [(+3)] <*> [2,3,4] Now, I also expect I can do the same thing as monad, it means I can apply 2 parameters: a context contains a function, and another context to get a context. But for monad, I can't. All I need is to write an ugly function like this: [2,3,4] >>= (\x->[x+3]) Yes, of course, you can say that [(+3)] is equivalent to [\x->(x+3)] . But at least, this function is in context. Finally, I don't see the equivalence or extension

monoid vs monad in Scala

只愿长相守 提交于 2019-12-03 08:26:42
问题 I have recently tried to find a good source on the difference between monads and monoids. Could someone provide a link to a good resource on this or perhaps take one's time to elaborate on the similarities/differences? 回答1: Monads are monoids in the category of endofunctors. Therefore, a monad is just one example of monoid, which is a more general concept. And, though that might be technically true, the most simple answer is that monads and monoids are really nothing like each other, and you

Using servant with ReaderT IO a

你。 提交于 2019-12-03 07:58:08
I'm using the servant library for my JSON API. I need some help to get a ServerT MyAPI (ReaderT a IO) monad stack working. Here's an example using ReaderT , but without integrating it with servant: -- this code works type TestAPI = "a" :> Get '[JSON] String :<|> "b" :> Get '[JSON] String test2 :: EitherT ServantErr IO String test2 = return "asdf" testServer :: Int -> Server TestAPI testServer code = test :<|> test2 where test :: EitherT ServantErr IO String test = liftIO $ runReaderT (giveMeAMessage) code -- this is contrived. In my real application I want to use a Reader for the database

MonadFix instance for interpreter monad transformer generated by FreeT?

梦想的初衷 提交于 2019-12-03 07:18:20
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 prompt (f resp) instance MonadFix m => MonadFix (FreeT (InteractiveF p r)) m a) mfix = -- ??? I feel like

Using monads for trivial tasks like list manipulation?

守給你的承諾、 提交于 2019-12-03 07:11:17
Whenever I read about Monad example, they always present IO as a case study. Are there any examples of monads doing list manipulation which somebody could present? I aprpeciate this could be overkill, but I am interested if monads can present advantages over regular list manipulation techniques. The big secret to the list monad in Haskell is that list comprehensions are syntactic sugar for do blocks . Any time you write a list comprehension, you could have written it using a do block instead, which uses the list monad instance. A simple example Let's say you want to take two lists, and return

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

旧城冷巷雨未停 提交于 2019-12-03 06:56:07
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 = map fromJust . takeWhile isJust . loadFiles So far, so good. The only problem is that, since

Using Cont to acquire values from the future and the past

帅比萌擦擦* 提交于 2019-12-03 06:56:06
问题 I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again.

Error handling monads in Scala? Try vs Validation

半腔热情 提交于 2019-12-03 06:53:29
问题 scalaz.Validation is said to be more powerful than the Try monad, because it can accumulate errors. Are there any occasions where you might choose Try over scalaz.Validation or scalaz.\/ ? 回答1: The most significant argument in favor of Try is that it's in the standard library. It's also used in the standard library—for example the callbacks you register with Future 's onComplete must be functions from Try . It may be used more extensively in the standard library in the future. The fact that

Precise flow control in Haskell

偶尔善良 提交于 2019-12-03 06:50:01
问题 The Idea Hello! I'm trying to implement in Haskell an image processing library based on dataflow ideology. I've got a problem connected to how I want to handle the flow of control. The main idea is to introduce a time . The time is a Float , which could be accessed anywhere in the code (you can think of it like about State monad, but a little funnier). The funny thing about it, is that we can use timeShift operation on results, affecting the time corresponding operations would see. An example