Stack Exchange Network

Stack Exchange network consists of 174 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.

0
votes
1answer
21 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
27 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
30 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
108 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
44 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
64 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
76 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
59 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
167 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
179 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. ...
1
vote
1answer
89 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'...
5
votes
2answers
189 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
115 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
76 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 <...
5
votes
1answer
779 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 (...
4
votes
3answers
216 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'...
4
votes
0answers
154 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
555 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
94 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
724 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
176 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 ...
5
votes
1answer
101 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
420 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
361 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
424 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
135 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
89 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
177 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
284 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
77 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
219 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
338 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
95 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
77 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
253 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
39 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
148 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
296 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 ...
7
votes
2answers
187 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
98 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
58 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 ...
11
votes
0answers
444 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
55 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
55 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 ...
10
votes
1answer
159 views

Probably unneeded returns

This is my first "useful" Haskell program. It's invoked with filenames as arguments where the last filename is the destination to which the content of all other files will be concatenated. As far as I ...
8
votes
2answers
4k views

An Either Monad in Java

Interested in a review of this code, in particular the (hopefully monadic) bind. I actually put this to good use. It was a nice complement to Guava's Optional, and shorter than: Optional....
8
votes
1answer
192 views

Monadic Immutable Linked List in the Least Functional Language Evar

I've written a List monad as an example for a related question. This is a rather frustrating experience as I wanted to use Java, which (as of Java 7) still lacks lambda expressions and lacks higher-...
6
votes
2answers
303 views

A monad in Java (FriendSpace and People)

After asking a similar question on Stack Overflow, I'm picking up on the answer there to improve my monad. I'm not trying to solve the general case, just come up with one to see how it works. If I'm ...