background preloader


Facebook Twitter

Sententia cdsmithus. (A Side Note: I’ve been formulating the final thoughts on this post for about a week now.

Sententia cdsmithus

In an entirely unrelated coincidence, a good friend of mine and fellow Haskell programmer, Doug Beardsley, ended up writing two posts about monads over the weekend as well. Weird! But don’t fret; this isn’t really the same thing at all. I’m not writing to teach Haskell programmers how to use monads. I’m writing about a kind of intuition about why these concepts turn out to matter in the first place. Category Theory for Software Development? Match made in heaven?

Haskell - State and IO Monads. All About Monads. All About Monads is a tutorial on monads and monad transformers and a walk-through of common monad instances.

All About Monads

You can download a PDF version here. And here is a version of the article which includes source code. Attempts are being made at porting the tutorial to this wiki; what you're seeing below is a preview of the result of that effort. If you wish to help out you should fork this GitHub repo rather than edit this page, for now. Haskell/Understanding monads/State - Wikibooks, open books for an open world. If you have programmed in any other language before, you likely wrote some functions that "kept state".

Haskell/Understanding monads/State - Wikibooks, open books for an open world

For those new to the concept, a state is one or more variables that are required to perform some computation but are not among the arguments of the relevant function. Object-oriented languages, like C++, suggest extensive use of state variables within objects in the form of member variables. Programs written in procedural languages, like C, typically use variables declared outside the current scope to keep track of state. In Haskell, however, such techniques are not as straightforward to apply. They require mutable variables and imply functions will have hidden dependencies, which is at odds with Haskell's functional purity. Pseudo-Random Numbers[edit] Generating actual random numbers is far from easy. Implementation in Haskell[edit]

Anton-k/monads-for-drummers. How I learned to understand Monads : haskell. LTMT Part 3: The Monad Cookbook. Introduction The previous two posts in my Less Traveled Monad Tutorial series have not had much in the way of directly practical content.

LTMT Part 3: The Monad Cookbook

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.

Monads, lifting, join, and side-effecting actions.

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. Also binding the query I want to perform, “find all documents with the field port equal to 3000, return 1 document”. simpleHTTP (getRequest url) >>= liftM putStrLn . getResponseBody But that doesn’t print anything. Λ> :t simpleHTTP (getRequest url) :: IO (Network.Stream.Result (Network.HTTP.Response String)) λ> :t simpleHTTP (getRequest url) >>= getResponseBody :: IO String So we’ve got an IO action returning a String.

For our purposes, fmap and liftM mean the same thing. The above is the right answer! 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.

IO: You may say I'm a monad, but I'm not the only one!

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.

So today we’ll take a look at practical uses of the monad-ness of types other than IO: in particular, list and Maybe. Speaking of Applicative and Functor, I’ll also be introducing some of those other computation abstractions, and showing you the same code using several different styles. 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.

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

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. 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.

Abstraction, intuition, and the “monad tutorial fallacy”

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. Concrete monads 1 : Maybe, Either, list, IO. Why Do Monads Matter? « Sententia cdsmithus. YouTube.