Haskell Monad State: gets and modify

I'm confused with the definitions of gets and modify in monad state. Somewhere says: gets: "Gets specific component of the state, using a projection function supplied". modify: Maps an old state to a new state inside a state monad. The old st...
more »

2017-07-16 20:07 (1) Answers

Monad vs Monad transformers

"Monads allow the programmer to build up computations using sequential building blocks" therefore it allows us to combine some computations. If this is the case, then why the following code can not be run? import Control.Monad.Trans.State gt :: St...
more »

2017-07-13 15:07 (1) Answers

StateT over Cont. Why is my state not being reset?

I'm playing with the Cont monad tricks described here and in this SO question. This function lets you "jump back" to earlier in the computation, taking a parameter so you can do things differently: import Control.Monad.Cont import Control.Monad.Sta...
more »

2017-07-08 18:07 (1) Answers

Is this an appropriate use of ContT?

I'm working on a project that requires me to write a small interpreter. The instructions have a simple tree structure and one of the commands has the effect of halting execution. So in the example below, "baz" is never printed. import Control.Monad....
more »

2017-07-03 05:07 (1) Answers

Write to multiple files in Haskell

In Haskell, how would one go about writing to an arbitrarily large number of files? As an example, let's say I want to take the letters a through z and put them in files named for the letter of their contents. An initial attempt was to do the follo...
more »

2017-06-29 11:06 (1) Answers

Haskell Type Error when Defining Monad

I have a type Effect which represents an effect on a stack. An Effect can either be a successful effect which modifies a stack and optionally generates output, or it can be an error that remembers how the stack looked when it failed. data Effect a =...
more »

2017-06-27 09:06 (1) Answers

Sugaring monadic code from Reader monad

I wrote some monadic code using a reader monad and I wonder if there is any way to write using the do-notation: m = Map.fromList [("This","is"), ("is","the"), ("the","secret")] f x m = fromMaybe "default" $ Map.lookup x m r :: String -> Reader (...
more »

2017-06-27 01:06 (2) Answers

Haskell non type variable argument

I try to work with the Reader Monad m = Map.fromList [("a","b"), ("b","c"), ("c","d"), ("d","e")] f x m = fromMaybe "default value" $ Map.lookup x m lookup' x = f x m Now I wanted to create a reader monad: r = reader lookup' -- Non type variable...
more »

2017-06-27 00:06 (1) Answers

How does this weird monad bind work?

This is probably a very noob question but I was playing around with the bind operator in Haskell and I encountered a way to repeat a string using it. [1..3] >>= const "Hey" -- Yields "HeyHeyHey" [1..3] >>= return "Hey" -- Also yields the...
more »

2017-06-24 01:06 (2) Answers

Newtype Deriving Monad Error

I'm trying to install an application Termite - Util but I get this error in with the line 14 newtype CE m a = CE {unCE :: m a} deriving (Monad) The error says: ContextError.hs:14:45: No instance for (Applicative (CE m)) arising fro...
more »

2017-06-20 10:06 (0) Answers

Wrap State result in a Just

I have a Haskell function in my Prolog interpreter assignment that reads renames :: Rule -> State ExecState [(Term, Term)] which produces (almost) a substitution of variables in a Prolog rule (so that variable names don't clash during unificati...
more »

2017-06-12 10:06 (1) Answers

More idiomatic way of exiting mapM early

Given a list of actions that returns m (Maybe a) I'm trying to return m (Maybe [a]) where if any of the individual results are Nothing the whole result is Nothing. m contains StateT and I want to avoid running any actions after the first Nothing is r...
more »

2017-05-26 15:05 (1) Answers

Changing function to Maybe function in Haskell

Is it possible to write a function which takes a function like (Something -> Something), and returns it, but with a type (Maybe Something -> Maybe Something)? e.g.: f :: Point -> Point f x = [some code goes here] makeItMaybe :: (Point -&g...
more »

2017-05-26 00:05 (3) Answers

How to use the Reader Monad with (Int -> Int)?

I would like to learn, how to use the Reader Monad. Unfortunately only a small amount of example code is available I would like to create a Reader, where the environment and the retrived values are Int. I defined the type like this: type IntRead = ...
more »

2017-05-23 20:05 (1) Answers

Using a Monadic eDSL from the REPL

Say I have created myself an embedded domain specific language in Haskell using a monad. For example a simple language that lets you push and pop values on a stack, implemented using the state monad: type DSL a = State [Int] a push :: Int -> DSL...
more »

2017-05-04 20:05 (2) Answers

Nothing in MaybeT monad: more concise way?

I'm experimenting with the MaybeT monad, specifically MaybeT Identity String import Control.Monad.Trans.Maybe import Control.Monad.Identity import Data.Maybe main :: IO () main = putStrLn . show . runIdentity . runMaybeT $ maybeGetString maybeGetS...
more »

2017-05-03 09:05 (0) Answers

Applicative instance requires 1 AND 0 arguments

I was going through "Learn Yourself a Haskell" and am trying to implement the "Heathrow to London" problem [1] in a Monadic way (instead of folds or explicit recursions). [1] http://learnyouahaskell.com/functionally-solving-problems#heathrow-to-lond...
more »

2017-04-27 23:04 (1) Answers

Mappend (a1,b1) and (a2,b2) into (a1+a2, b1+b2)

I recall this is very basic, and could simply be done with pattern matching even through a lambda like (\a b -> (fst a + fst b, snd a + snd b) ) (1,2) (3,4). However, I think there should be ways provided by the Haskell standard library to do thi...
more »

2017-04-27 22:04 (3) Answers

Haskell <- in do notation

It's well known that <- in do blocks are just syntactic sugar for >>=, but is <- defined anywhere in the Haskell source or is it just syntactic construction that is part of language grammar and thus the parser just replaces every <- wi...
more »

2017-04-26 12:04 (1) Answers

GetLine within list comprehension

I want to build a matrix in a list comprehension. For each row I need to getLine and parse it out. In short, I want to do something like this. fun = [ getLine | y <- [0..4]] If I run this function, instead of trying to getLine from command pro...
more »

2017-04-21 06:04 (1) Answers