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

Swapping `mappend` in Writer monad

Summary: While using Writer monad, I would like to be able to switch between 2 different versions of mappend without losing the state. I use two boolean flags to track some state: data Flags = F Bool Bool Now I define two Monoid instances which d...
more »

2017-04-19 03:04 (1) Answers

Typeclass instances for functions

I've just realized, that Functions have instances for Monad, Functor and Applicative. What I usually do, when I see some typeclass instance I don't get, is write some well-typed expression and see what it returns: Can somebody explain these instanc...
more »

2017-04-12 22:04 (2) Answers

Difference between >>= and concatMap

I was playing around with >>= today, trying to understand monads, and found an interesting pattern. When working with the list monad, >>= seemed to behave like concatMap. I searched around to try to find any similarity, looking specifical...
more »

2017-04-12 21:04 (2) Answers

Moving piece across a board in Haskell

I'm working on a chess game in Haskell and I'm struggling with moving my pieces. I understand that in functional programming, everything should be immutable, but I think I really need to have an updated list of pieces. I looked at monad.state but I'...
more »

2017-04-09 17:04 (1) Answers

Advice on writing monadic signatures

Consider the following example functions which both add a random value to the pure input: addRand1 :: (MonadRandom m) => m (Int -> Int) addRand2 :: (MonadRandom m) => Int -> m Int -- *can* be written as m (Int -> Int) It is easy to...
more »

2017-04-07 16:04 (4) Answers

How to read the bind operator ">>=" in words?

When reading code, I like to be able to form sentences in my head. For example, x <- getChar could be something like "x slurps getChar". Or a . b would be "b applied to a". But when it comes to the Monadic Bind operator f >>= g, I just leav...
more »

2017-04-07 08:04 (3) Answers

Validating list of strings

This is a follow up to my previous question. Suppose I need to write a function validate in order to make sure that a given list of strings consists of "a", "b", and one or more "c". def validate(ss: List[String]): Either[NonEmptyList[MyError], Unit...
more »

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

examples in the paper Monadic Parse in Haskell

In the paper Monadic Parse in Haskell, the author gives an example about parsing string of simple arithmetics. I tried to expand the term, applied it to "1 + 2", but I'm still confused about the recursive nature of the parsers. That is, the term woul...
more »

2017-03-29 13:03 (1) Answers

How do I combine Result and State in Elm?

I am struggling with Elm's lack of monads. A library implementing the state monad for Elm (http://package.elm-lang.org/packages/folkertdev/elm-state/latest/State) has helped me quite a bit. The problem is that I have now run into a situation where I...
more »

2017-03-28 11:03 (2) Answers

Purpose of Reader Monad

I am having difficulty in understanding the Reader Monad. My understanding from all the places I read, is that it allows to share a variable(Read Mode) to different function. Below is the implementation of two functions computation and samething (sor...
more »

2017-03-23 17:03 (2) Answers

How infinite loop with IO work in haskell

In haskell, all the IO code that we write is just an action (many suggest to think it as a script being generated). It is the main method that finally executes them (execute the constructed script). So how does the following program work ? infi funct...
more »

2017-03-22 19:03 (4) Answers

Recursive liftIO

I've looked at the some instances of MonadTrans, for MaybeT the implementation looks like this: instance MonadTrans MaybeT where lift = MaybeT . liftM Just As I understand the instance for MonadIO is used to do a variable number of lifts from ...
more »

2017-03-21 12:03 (2) Answers

Which Algebraic Pattern fits this type of tree?

I've got a puzzle for you, I managed to write some code that would do these things using recursion-schemes, but it's incredibly messy and that usually means that I missed a useful abstraction somewhere. I'm designing a layout system for my text edi...
more »

2017-03-19 20:03 (2) Answers

Ambiguous occurrence `Monad'

I am currently working with Monad type classes, and here is my code: class Monad m where return :: a -> m a (>>==) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b x >> y = x >>...
more »

2017-03-19 13:03 (0) Answers

Why does this haskell code failed

First of all I don't exactly understand the error message, so I can only use a ambiguous question title This code is from Chapter 8 of Channel 9 lecture about programming in haskell I was trying to run this code by GHC and I wrote -- Functional p...
more »

2017-03-14 10:03 (1) Answers