Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Questions tagged [monads]

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

Filter by
Sorted by
Tagged with
0 votes
0 answers
46 views

Typescript Maybe functor

I have been trying to implement a version of the Maybe functor in Typescript. I'm looking for feedback on whether I can improve the type safety of my implementation. I'm not particularly happy that ...
user avatar
1 vote
0 answers
40 views

Count number of days within each date range in a list of date ranges

Happily picked up Haskell a couple days back and working on the following use case. Given a list of JSON objects with date fields and a start date, I want to create a list of weeks (here a tuple of ...
user avatar
6 votes
2 answers
1k views

Hamming distance between two strings

I wrote this module to find the Hamming distance between two strings. (It's a problem from exercism.io's Haskell track.) As I saw it, the problem has two distinct ...
user avatar
7 votes
1 answer
212 views

A beginner's brainfuck interpreter in Haskell

I am rather new to haskell, and could use some feedback on my code and the decisions I had to make. In my previous project, I made a JSON parser, but relied heavily on guidance from a university ...
user avatar
3 votes
1 answer
42 views

Haskell - saving content of HTTP request in selected file

As a Haskell beginner I'm currently playing around the IO monad and prepared simple program which gets from the user two information: URL (http) Output file path Then it performs ...
user avatar
  • 143
1 vote
2 answers
190 views

Either monad in C++11

I'm using C++11 with https://github.com/SmingHub/Sming. It has no support of std lib or exceptions. I wanted to use Either monad ...
user avatar
  • 113
11 votes
1 answer
374 views

Telegram bot in Haskell using custom monad transformers

Note: I show almost all of my code for completeness, but I really only want the review to focus on Session.hs, Handler.hs, and maybe Controller.hs. I can delete the extra code from the review or ...
user avatar
  • 176
2 votes
1 answer
364 views

How can I use the State monad effectively in Haskell?

I have written a simple fibonacci function in Haskell that uses the State monad to save solutions to overlapping sub-problems. Now despite the function working I am not really happy with the solution, ...
user avatar
  • 495
4 votes
1 answer
301 views

Loading Files from a Directory in Haskell

I'm a very new Haskell programmer, and I've written the following code for loading TSV files. I think it can be improved, but I'm not sure how. I feel like the giant "do" block in loadData is ...
user avatar
1 vote
1 answer
92 views

Monad to sample without replacement

I created a monad in Haskell that lets you sample without replacement from user-defined urns, and then at the end gives you a list of all possible outcomes. It looks like it's similar to the list ...
user avatar
5 votes
1 answer
95 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 (...
user avatar
6 votes
1 answer
192 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 ...
user avatar
4 votes
2 answers
131 views

Implementation of partitionM

I needed a monadic version of partition today. I settled on the following version: ...
user avatar
  • 143
6 votes
1 answer
1k 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 ...
user avatar
  • 317
0 votes
1 answer
42 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 ...
user avatar
1 vote
1 answer
37 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 ...
user avatar
1 vote
1 answer
94 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, ...
user avatar
  • 317
3 votes
1 answer
137 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 ...
user avatar
  • 1,538
2 votes
0 answers
54 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 ...
user avatar
  • 121
7 votes
1 answer
108 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 ...
user avatar
  • 205
2 votes
1 answer
128 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, ...
user avatar
8 votes
1 answer
503 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 ...
user avatar
6 votes
1 answer
652 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. ...
user avatar
  • 193
2 votes
1 answer
125 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'...
user avatar
  • 249
6 votes
2 answers
408 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 ...
user avatar
  • 249
1 vote
1 answer
177 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: ...
user avatar
  • 261
5 votes
1 answer
304 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 <...
user avatar
  • 153
4 votes
1 answer
1k 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 (...
user avatar
3 votes
3 answers
458 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'...
user avatar
3 votes
0 answers
189 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 ...
user avatar
  • 31
4 votes
3 answers
2k 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: ...
user avatar
3 votes
2 answers
116 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: ...
user avatar
  • 133
2 votes
1 answer
1k 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. ...
user avatar
6 votes
1 answer
336 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 ...
user avatar
  • 273
6 votes
1 answer
131 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 ...
user avatar
1 vote
1 answer
614 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 ...
user avatar
  • 171
2 votes
1 answer
420 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 ...
user avatar
7 votes
3 answers
908 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 ...
user avatar
  • 203
7 votes
2 answers
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 ...
user avatar
5 votes
1 answer
148 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 ...
user avatar
4 votes
1 answer
105 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: ...
user avatar
  • 1,159
4 votes
1 answer
236 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 ...
user avatar
3 votes
1 answer
389 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: ...
user avatar
5 votes
1 answer
110 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 ...
user avatar
5 votes
2 answers
386 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 ...
user avatar
8 votes
1 answer
360 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 ...
user avatar
5 votes
1 answer
112 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 ...
user avatar
  • 301
2 votes
1 answer
85 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: ...
user avatar
5 votes
2 answers
292 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 ...
user avatar
3 votes
1 answer
54 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 ...
user avatar
  • 187