Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [monads]

A monad in programming is a composable computation description. Monads are an important construct in functional programming languages like Haskell.

4
votes
0answers
17 views

Typescript monad for data being loaded

I have been struggling to make typescript happy about the types. I am pulling data from our backend API and I wanted to give context to the data. Basically it is a monad with 4 shapes: Initial (...
6
votes
1answer
107 views

C++17 Either implementation for error handling [closed]

This is a C++17 Either implementation for error handling. First intent is I want to improve myself. Second intent is I want to create a more expressive solution than variant for exception free error ...
4
votes
2answers
104 views

Implementation of partitionM

I needed a monadic version of partition today. I settled on the following version: ...
4
votes
1answer
145 views

Writing monadic Haskell to evaluate arithmetic expression

I have been studying Haskell by myself for about a little over a year. And I have been stuck at monad/monad transformers for quite a while until recently some examples I read online enlightened me. So ...
0
votes
1answer
30 views

Using foldl to add numbers, while validating that they are nonnegative

I have a function that will calculate the sum of a list of integer, however if any of the number is negative, it will should an error instead (not throwing exception). The following is a working ...
1
vote
1answer
32 views

Stack Exchange API Reader

I've been working on some code to pull questions from the realtime feed on stackexchange.com and query more information about them from the API. It works, but I'd love some feedback on how I could ...
1
vote
1answer
42 views

Implement State Monad transformer in Haskell from scratch

When I was studying Monad Transformer, I decided to create StateT s m a from scratch with instances for Functor, ...
3
votes
1answer
117 views

Determining whether (x,y) points in the input constitute a function

I'm learning Haskell, and I've finally gotten around to coding up some Haskell. This code passed my tests. This code takes a line from standard in which tells it how many cases it's going to have (1 ...
2
votes
0answers
48 views

Monadic implementation of asynchronous tasks (hidden overhead of threads)

So, I've been reading about monads and I wanted to see if I could implement a system for asynchronous computation in a monadic way. I came up with two solutions: The first one spawns a thread for ...
7
votes
1answer
71 views

Monadic list for a more functional approach to C#

I am learning about monadic structures and have created a sort of monad list that implements the map and binding "operators" (or technically methods) in C#. I understand that Linq already exists and ...
2
votes
1answer
88 views

Haskell/Yesod - Butchering use of MaybeT

The following is some code that is used to provide the frontend with certain parameters when rendering a form. The parameters are conditional based around what data is being changed, ...
4
votes
0answers
63 views

Functional implementation of the Maybe Monad in JS [closed]

A while ago, I really wanted to have the Maybe Monad in my JS projects. I ended up trying to use Promises as a stand-in solution (using Promise.resolve as my unit, ...
7
votes
1answer
211 views

F# Either computation expression with while loop

I want to implement an Either computation expression in F#. The computation executes a function inside a while loop, and this function's return type is ...
6
votes
1answer
254 views

State Monad in TypeScript

Here's my implementation of State Monad in TypeScript, based on a canonical Haskell implementation. I would like it to get code reviewed. ...
2
votes
1answer
101 views

Monad Transformers

I've run into the same scenario involving Monad Transformers a few times now, and I'm looking for some "best-practice" advice. Bear with the introduction, the issue itself is really simple: Say you'...
6
votes
2answers
241 views

Simple DFA using the State Monad

I've been getting to grips with some Monads in Haskell recently, and to get some practice I plucked a Mealy Machine out of thin air and decided to implement it using the State Monad (it's a simple ...
1
vote
1answer
135 views

Bubblesort using Writer monad

I still consider myself a newbie to functional programming, and I still have some trouble wrapping my head around a lot of functional concepts. Anyway, here goes: ...
5
votes
1answer
111 views

Haskell limit number of recursions

I want to create a monad which will prevent more than 10 instances of recursion (which I am implementing by preventing more than 10 instances of >>=). My <...
4
votes
1answer
911 views

Monad Implementation in Typescript

Having been introduced to monads through Scala, I am trying to add the generalized design pattern to my toolkit by implementing it in a language I use more regularly (typescript). My tests pass (...
3
votes
3answers
253 views

Haskell Sudoku Solver

Any comments on the following Sudoku solver? Comments I'm particularly interested are (but not limited to...) Algorithm. It created a list of "potentials" for each cell, and trims them down until it'...
3
votes
0answers
168 views

Sudoku solver in Haskell using Reader+ST

While trying to understand monad transformers in Haskell I came up with a sudoku solver based on a combination of the ST and Reader monad. I am positively surprised with the performance, however ...
4
votes
2answers
687 views

Leap year check in Haskell, using pattern matching or bind operator

One of the first Haskell puzzles on http://exercism.io/ is to implement a leap year check. I've done this twice. Using pattern matching: ...
3
votes
2answers
96 views

Monadic transformation on a list, done in three ways

I am working my way through the Haskell Book and ran into the exercises at the end of the Monad chapter(18). One in particular asked to define a function with the following signature: ...
1
vote
1answer
855 views

CompletableFuture Chain (Monad)

I wrote an application that has a modular structure. Each module communicates with different remote servers. Time for execution of a task in each module varies. So communication should be asynchron. ...
6
votes
1answer
208 views

Algebraic Effect Monad to separate pure and impure code

I want to write my C# code in a way which makes effects (state, I/O, etc) explicit in the type signature. I have started to understand monads, and have some acquaintance with algebraic effects, though ...
6
votes
1answer
106 views

Using monads and monad transformers (simple matching engine)

Like most of the folks here, I'm learning Haskell, so I figured an interesting exercise would be to build a matching engine that I could use to build a financial exchange. I'm wondering primarily if I ...
1
vote
1answer
475 views

Composing IO and Async in haskell

Whether based on MVar or TVar, async implementation are always based on operation on some underlying monad IO and STM. Making Async a monad on its own, as in F# async computation builder, if done in ...
2
votes
1answer
378 views

Transforming XML to HTML with XSLT

I'm still learning from designing with types non strings by Scott Wlaschin. For my project I transform some XML to HTML with xslt. Because every filename can have side effects, they are stored in ...
7
votes
3answers
484 views

Prolog parser written in Haskell

I am writing a very simple Prolog intepreter in Haskell. It's a class assignment and I really want to do it right. I was able to (quite quickly) write a parser for the language. Today I borrowed a ...
7
votes
2answers
1k views

Struct implementation of immutable Maybe<T> monad

I have developed the following implementation of an immutable struct Maybe<T> monad for use in my game project; as an ...
5
votes
1answer
137 views

CIS194: Risk!-style “battles”

After reading LYAH and using RWH as a reference, I've been doing the exercises from CIS194, which is often suggested to beginners on irc://freenode.net/#haskell and ...
4
votes
1answer
92 views

Hacky Haskell monadic testing

Edit for posterity: The library you're looking for is Hspec. I recently started writing Haskell tests with HUnit. My general setup looks something like this: ...
4
votes
1answer
196 views

DSL design — C DSL

I am trying to design a DSL for writing C. Here is my code as a GitHub gist (Github does not allow me to have slashes in the filenames so I have used underscores instead). It depends on packages free ...
3
votes
1answer
300 views

Simple/Naive Implementation of Identity and Maybe Monad in Elixir

This is my first crack at a identity monad and a maybe monad in Elixir: ...
5
votes
1answer
86 views

Type Validation using State Monad

I am attempting to build a compiler for a stack-based (or concatenative) programming language in Haskell. I've done fine getting the lexer and parser up and running, but now that I'm into the meat of ...
5
votes
2answers
227 views

Applicative permutation to generate knight moves

I want to understand Haskell better by trying to stretch possibilities of Functor, Applicative, and Monad as much as possible and study how they behave. So in Learn You a Haskell there's an exercise ...
8
votes
1answer
342 views

Functional Programming Library

I have started putting together a functional programming library in C# which is somewhat inspired by scalaz and the book Functional Programming in Scala. I am hoping to get some feedback on the Monad ...
5
votes
1answer
96 views

Safe stack operations with lens

I wrote this code to learn how to work with State monad. Then I added Either result to gracefully handle error condition (stack underflow). I believe most of this code is actually unnecessary. I know ...
2
votes
1answer
79 views

Polymorphic components for graphics and program state

I asked this question on StackOverflow, got some answers, most notably a link to this one, and basing on that I've implemented this: ...
5
votes
2answers
267 views

Checking in four directions for board game win

I have the following code which I would like to simplify in Haskell, although I'm not sure how. I recall that I can use monads to simplify a case chain when the result of the case leads onto a next ...
3
votes
1answer
41 views

Haskell: downpair :: (Monad m) => m (a, b) -> (m a, m b) implementation

I'm trying to implement function downpairs which takes monad of pairs and transform it into pair of monad My current implementation is ...
5
votes
1answer
199 views

Popping from a list in state while a condition is true

I'm dealing with data that stores its state as a String, treating the string like a stack. I also have to combine that with error handling. To that end, I'm using ...
2
votes
2answers
328 views

Haskell monads: sum of primes

They say you cannot get out of the monad, but I need to use the result (at least for assertions / unit tests). But print how does it do it? Otherwise, I worked ...
9
votes
2answers
206 views

Random numbers, IO monad, and pure code

To get used to Haskell, I wrote a little program using the Diagrams package which generates 100 random dots (larger dots are more likely to be in the center). ...
2
votes
0answers
45 views

Expressing computations on values as State

I was writing an application with a few Monads in a transformer stack. The top level application state resides in a TVar, and various components of the application ...
4
votes
2answers
99 views

Haskell monad- and error-handling-style

As an answer to excersise 2.3 in Chris Okasaki's book, Purely Functional Data Structures, Inserting an existing element into a binary search tree copies the entire search path even though the ...
3
votes
0answers
61 views

Abstract Key Value Store

I'm attempting to define an abstract interface for a key value store. Some requirements: Once a key has been written it should never be overwritten (although it may be deleted Transactions with ...
12
votes
0answers
488 views

Binary Search Tree Monad Implementation

I've written a Binary Search Tree Monad in Scala. I would like to hear your thoughts on how to improve it (e.g. making insertion/deletion/search faster and more scalable). Also, is there a better way ...
3
votes
1answer
58 views

v2 - Adding a duplicate entry randomly into a list in haskell using random monad

Next version of Adding a duplicate entry randomly into a list in haskell using random monad I wrote this trying to set up a Haskell testcase. The aim is to take a list and add a single duplicate from ...
4
votes
1answer
58 views

Adding a duplicate entry randomly into a list in haskell using random monad

There's a new version of this as v2 - Adding a duplicate entry randomly into a list in haskell using random monad I wrote this trying to set up a Haskell testcase. The aim is to take a list and add ...