background preloader

You Could Have Invented Monads! (And Maybe You Already Have.)

You Could Have Invented Monads! (And Maybe You Already Have.)
If you hadn't guessed, this is about monads as they appear in pure functional programming languages like Haskell. They are closely related to the monads of category theory, but are not exactly the same because Haskell doesn't enforce the identities satisfied by categorical monads. Writing introductions to monads seems to have developed into an industry. There's a gentle Introduction, a Haskell Programmer's introduction with the advice "Don't Panic", an introduction for the "Working Haskell Programmer" and countless others that introduce monads as everything from a type of functor to a type of space suit. But all of these introduce monads as something esoteric in need of explanation. But what I want to argue is that they aren't esoteric at all. Many of the problems that monads try to solve are related to the issue of side effects. Side Effects: Debugging Pure Functions In an imperative programming language such as C++, functions behave nothing like the functions of mathematics. Solution and

http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html

Related:  Functional languagesMonadsMonads

The State Monad: A Tutorial for the Confused? - Brandon.Si(mmons) I’ve written this brief tutorial on haskell’s State monad to help bridge some of the elusive gaps that I encountered in other explanations I’ve read, and to try to cut through all the sticky abstraction. This is written for someone who has a good understanding of the Maybe and List monads, but has gotten stuck trying to understand State. I hope it’s helpful! The Data Declaration: To understand a monad you look at it’s datatype and then at the definition for bind (>>=). Introductions to advanced Haskell topics Many people bemoan the sharp divide between experts and beginning Haskell programmers. One thing I've noticed is that "advanced" Haskell topics all have one thing in common: there exists only one good tutorial on that topic and only the experts have found it. This post is a collection of links to what I consider to be the definitive tutorials on these topics. Monads Monads are technically not an advanced Haskell topic, but I include this topic in the list because the vast majority of monad tutorials are terrible and/or misleading.

IO: You may say I'm a monad, but I'm not the only one! One of the things I love about Haskell is its tools for abstracting computation. There are so many different ways to look at a given problem, and new ones are being invented/discovered all the time. Monads are just one abstraction, a popular and important one. When you’re learning Haskell, you’re told ’IO is a monad’, and whether or not you understand what that means, you start to see the significance of binding impure values, returning pure ones, using do notation, and so on. I thought I was a pro at monads after discovering liftM, but I still hadn’t really made the jump to understanding monads in general, rather than as something specific to impure IO operations.

The sequence monad I'm currently reading the excellent tutorial on monads here: and paraphrasing it to help me understand. You may prefer to look at my earlier post first. This is a follow-up to that. We've already seen that ((fn [a] ((fn [b] (* a b)) 2)) 1)

Why I’m betting on Elixir Background: I’ve spent the past 6 years building web applications in Ruby and the Rails framework. I’ve flirted with new programming languages as they came out, but Elixir is the first language that has been able to captivate me. Ruby Crushed It! The ruby language and the Rails framework completely changed the way web applications were built; it started a religion of values that the community cared about. It pioneered the idea that the tools programmers use should be optimized for developer happiness and productivity. It made the bold challenge that it is developers’ jobs to make sure their code was adequately tested and worked.

Haskell/Understanding monads Monads are very useful in Haskell, but the concept is often difficult at first. Since they have so many applications, people often explain them from a particular point of view, and that can confuse your understanding of monads in their full glory. Historically, monads were introduced into Haskell to perform input/output. Abstraction, intuition, and the “monad tutorial fallacy” While working on an article for the Monad.Reader, I’ve had the opportunity to think about how people learn and gain intuition for abstraction, and the implications for pedagogy. The heart of the matter is that people begin with the concrete, and move to the abstract. Humans are very good at pattern recognition, so this is a natural progression. By examining concrete objects in detail, one begins to notice similarities and patterns, until one comes to understand on a more abstract, intuitive level. This is why it’s such good pedagogical practice to demonstrate examples of concepts you are trying to teach. It’s particularly important to note that this process doesn’t change even when one is presented with the abstraction up front!

Programming with impossible functions, or how to get along witho A recent paper by Hyland and Power speculates about an alternative universe in which languages like Haskell develop along a path in which monads don't play a starring role. This post will be about making tentative steps in that direction to see what developer-friendly alternatives might exist to solve problems like writing code using I/O in an elegant and readable manner. I think all of what I'll say was worked out 20 years or more ago but there may be some good reasons to think about this stuff again.

Elixir, ReactJS, and the Technologies That Will Rule Until 2020 My name is Ken Mazaika and I’m the CTO & Co-founder of The Firehose Project. I’m a strong believer in the idea that the software development environment is evolving at such a fast pace that it’s far more practical to learn the fundamentals instead of the next group of cutting edge technologies. However, beginners will often ask about the future of web development. And it makes sense. Haskell/Monoids In earlier parts of the book, we have made a few passing allusions to monoids and the Monoid type class (most notably when discussing MonadPlus). Here we'll give them a more detailed look and show what makes them useful. What is a monoid?[edit] The operation of adding numbers has a handful of properties which are so elementary we don't even think about them when summing numbers up.

Monads, lifting, join, and side-effecting actions. While playing around with querying ElasticSearch I bumped into something that I hadn’t really understood explicitly before about monads, nesting, and IO. Rather than blather on, I’m going to share a “literate” ghci session that demonstrates the point. Main editing change I made was to remove duplication in the output from querying the type :t in ghci. import Network.HTTPimport Control.Monad (join, liftM) let url = " Importing the HTTP client library, a simple one based on IO.

the Monad I'm currently reading the excellent tutorial on monads here: and paraphrasing it to help me understand. The simplest monad is let, the identity monad. (let [a 1] (let [b 2] (let [c (* a b)] (let [d (* a a)] (let [e (+ (* b b) (/ c d))] (let [f (+ c d e)] (let [g (- c)] (* a b c d e f g)))))))) The let above represents a complex computation. During the computation, values are computed, names are bound to values, values are used as inputs to functions. Values could also be pulled in from the global namespace, and side effects could be produced.

Related: