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

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