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
5 votes
1 answer
873 views

Monad object in C++

Have I defined a Monad object for C++? Below is a class called var which I created years ago to implement a lambda calculus like interpreter in C++. The class ...
StormCrow's user avatar
  • 369
5 votes
1 answer
135 views

Ackermann-Péter function call count using Writer monad

I'm quite new to Monads and I tried add function call counting to the Ackermann function code. The goal was simplicity, not performance. I want to have code review on the ...
Arpad Horvath's user avatar
5 votes
1 answer
133 views

Composite expected in C++

I'm interested in creating a successor to the expected monad in C++. Specifically I want it to be capable of storing error which may has one of many types, and a ...
Andrew's user avatar
  • 153
1 vote
1 answer
105 views

Hangman game in Haskell, written using a state monad

I recently made a simple hangman game in Haskell but soon wanted to explore the state monad, since it could possibly simplify the code. The result was this (see below), which is exactly 30% more lines ...
ola_bandola's user avatar
1 vote
0 answers
68 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 ...
clouddreams's 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 ...
Josh Friedlander's user avatar
7 votes
1 answer
332 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 ...
el_micha's user avatar
3 votes
1 answer
78 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 ...
trivelt's user avatar
  • 143
1 vote
2 answers
809 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 ...
Matzz's user avatar
  • 113
11 votes
1 answer
455 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 ...
Me again's user avatar
  • 176
2 votes
1 answer
670 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, ...
Orestis's user avatar
  • 495
4 votes
1 answer
455 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 ...
Erty Seidohl's user avatar
1 vote
1 answer
111 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 ...
Joseph Sible-Reinstate Monica's user avatar
5 votes
1 answer
140 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 (...
Mehdi Saffar's user avatar
6 votes
1 answer
274 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 ...
István Simon's user avatar
4 votes
2 answers
135 views

Implementation of partitionM

I needed a monadic version of partition today. I settled on the following version: ...
Roxy's user avatar
  • 143
6 votes
1 answer
2k 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 ...
dhu's user avatar
  • 327
0 votes
1 answer
54 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 ...
Wong Jia Hau's user avatar
1 vote
1 answer
41 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 ...
thesecretmaster's user avatar
2 votes
1 answer
157 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, ...
dhu's user avatar
  • 327
3 votes
1 answer
142 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 ...
Aaron Hall's user avatar
  • 1,558
2 votes
0 answers
60 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 ...
markonius's user avatar
  • 121
7 votes
1 answer
121 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 ...
NaN's user avatar
  • 205
2 votes
1 answer
147 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, ...
danbroooks's user avatar
8 votes
1 answer
584 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 ...
Carlos Rodriguez's user avatar
6 votes
1 answer
724 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. ...
dark_ruby's user avatar
  • 193
2 votes
1 answer
135 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'...
hnefatl's user avatar
  • 249
6 votes
2 answers
482 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 ...
hnefatl's user avatar
  • 249
1 vote
1 answer
194 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: ...
robbie's user avatar
  • 261
5 votes
1 answer
414 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 <...
Xodarap's 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 (...
Manningham's user avatar
3 votes
3 answers
525 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'...
Michal Charemza's user avatar
3 votes
0 answers
198 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 ...
curious's 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: ...
Matthias Wimmer's user avatar
3 votes
2 answers
125 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: ...
Carson's 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. ...
GreedyHat's user avatar
6 votes
1 answer
373 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 ...
Kazark's user avatar
  • 273
6 votes
1 answer
145 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 ...
Sameer Parekh Brenn's user avatar
1 vote
1 answer
707 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 ...
nicolas's user avatar
  • 171
2 votes
1 answer
433 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 ...
Peter Siebke's user avatar
7 votes
3 answers
974 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 ...
David's user avatar
  • 203
7 votes
2 answers
2k 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 ...
Pieter Geerkens's user avatar
5 votes
1 answer
153 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 ...
Tobia Tesan's user avatar
4 votes
1 answer
123 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: ...
wchargin's user avatar
  • 1,169
4 votes
1 answer
259 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 ...
Charles Cooper's user avatar
3 votes
1 answer
427 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: ...
Onorio Catenacci's user avatar
5 votes
1 answer
129 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 ...
Rob Kleffner's user avatar
5 votes
2 answers
419 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 ...
Mirzhan Irkegulov's user avatar
8 votes
1 answer
372 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 ...
Blair Davidson's user avatar
5 votes
1 answer
115 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 ...
sevo's user avatar
  • 291