Rerun computation after throwing error

I would like to combine the Error Monad and the IO Monad in Haskell. Here is an excerpt from my code: type AskMonad = ErrorT String IO askSomething :: AskMonad Direction askSomething = do liftIO $ putStrLn "Choose an element from the list [1,2,3,...
more »

2017-01-03 17:01 (1) Answers

Monad vs sequential function calls

The Wikipedia article on monads says: Purely functional programs can use monads to structure procedures that include sequenced operations like those found in structured programming. Not looking for an (another) monad tutorial here. Just please ...
more »

2016-12-28 02:12 (1) Answers

Monads, composition and the order of computation

All the monad articles often state, that monads allow you to sequence effects in order. But what about simple composition? Ain't f x = x + 1 g x = x * 2 result = f g x requires g x to be computed before f ...? Do monads do the same but with ha...
more »

2016-12-24 05:12 (3) Answers

Haskell getting value of Either from Get monad

I am a Haskell noob feeling stuck on a rather simple function I am attempting to perform. Ultimately my goal is to read a strict ByteString, use the Get monad with a decoder to retrieve the first Word32 from the ByteString, and perform specific func...
more »

2016-12-22 16:12 (1) Answers

How to use threadDelay inside a runContextT

I'm currently learning Haskell. (With GPipe) I have the following code which is part of a program that draws a triangle on the screen: drawyRun :: IO () drawyRun = do runContextT GLFW.newContext (ContextFormatColor RGB8) $ do threadDelay 10 ...
more »

2016-12-18 23:12 (0) Answers

ApplicativeDo not working with sequencing

I have this type, basically a Kleisli arrow: {-# language DeriveFunctor #-} data Plan m i o = Plan (i -> m o) deriving Functor instance (Monad m) => Applicative (Plan m i) where pure x = Plan (\_ -> pure x) Plan f <*> Plan ...
more »

2016-12-18 17:12 (2) Answers

Custom Testable instance with QuickCheck

I have a custom result/error monad, nothing fancy: import Control.Monad (liftM, ap) data MyResult a = Error String | Success a instance Functor MyResult where fmap = liftM instance Applicative MyResult where pure = return (<*>)...
more »

2016-12-16 11:12 (1) Answers

Why wrap an IO result in IO Monad

In Haskell we have a function readFile :: FilePath -> IO String. My question while understanding monad is why wrap it in IO? Couldn't we just have written function like these: (lines.readFile) path Rather than (readFile >>= lines) path ...
more »

2016-12-12 08:12 (3) Answers

Result of expressions in Haskell with monads

I'm currently preparing for my final exam regarding Haskell, and I am going over the Monads and we were giving an example such as: Given the following definition for the List Monad: instance Monad [] where m >>= f = concatMap f m return x...
more »

2016-12-12 00:12 (2) Answers

GHC 8.0.1 errors from Writer and State monads

I'm relatively new to Haskell and trying to port the UNIX wc program demonstrated in Kernighan and Ritchie using monadic composition, as shown in The Essence of the Iterator Pattern by Jeremy Gibbons and Bruno C. d. S. Oliveira, and having some troub...
more »

2016-12-10 11:12 (1) Answers

How to use monad in this case?

Say, I want to connect to a database and do some query. I decide to use monad to implement like this: queryDatabase :: IO String ... doSomeQuery :: IO () doSomeQuery = do conn <- connectToDatabase result <- queryDatabase conn re...
more »

2016-12-10 10:12 (1) Answers

Rebind do notation with typeclass-free monad

It is possible to rebind the (>>=) and return for a monad using explicit dictionary passing like this: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RebindableSyntax #-} module Lib where import Prelude hiding ((>>=), return) data MonadDict m = M...
more »

2016-12-07 17:12 (2) Answers

Distinguishing errors in haskell

I'm still new to Haskell. Whats the best way to distinguish between errors? Currently I'm using the maybe monad but it can only 'represent one state of error'. The following code snippet will put my question into context. pulOh :: Seq -- i...
more »

2016-12-01 13:12 (1) Answers

Monad "unboxing"

My question came up while following the tutorial Functors, Applicatives, And Monads In Pictures and its JavaScript version. When the text says that functor unwraps value from the context, I understand that a Just 5 -> 5 transformation is happening. ...
more »

2016-11-27 16:11 (6) Answers

I/O Monad and ByteString to Char conversion?

I'm testing some HTTP requests in haskell and have the below methods: import qualified Data.ByteString.Lazy as LAZ import Language.Haskell.TH.Ppr import System.IO import Data.Word (Word8) request :: IO LAZ.ByteString request = do response <...
more »

2016-11-24 23:11 (2) Answers

How are free objects constructed?

So I understand that a free object is defined as being the left-hand side of an adjunction. But how does that lead you to the Haskell definition of such objects? More concretely: given a "forgetful functor" from the category of monads to the categor...
more »

2016-11-20 13:11 (1) Answers

ApplicativeDo in Haskell

AFAIK one of the new additions to GHC8 is the ApplicativeDo language extension, which desugars the do-notation to the corresponding Applicative methods (<$>, <*>) if possible. I have the following questions. How does it decide whether th...
more »

2016-11-19 06:11 (1) Answers

Haskell: takeWhile on list of Maybe

An infinite list of Maybe n, which, from a specific position, has only Nothing items: someNumbers :: [Maybe Int] someNumbers = [Just 4, Just 3, Just 2, Just 1] ++ cycle [Nothing] I would like to count the ns statisfying a predicate p, stopping at ...
more »

2016-11-13 17:11 (1) Answers