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. 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. f,g :: Float -> Float f',g' :: Float -> (Float,String) which implies that Related:  Monads

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. 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. A quick review: functors A concept I’ll be referring to a lot in this post is the idea of computing on things in boxes. succ (Just 5) We can do this: Pow. Yup.

How would I even use a monad (in C#)? | Twisted Oak Studios Blog How would I even use a monad (in C#)? As you may have guessed, based on everyone ever trying to explain what they are, monads are an interesting concept. I assume the difficulty in explaining them stems from the high level of abstraction, compared to related ideas like “being enumerable”, but I won’t be trying to explain what a monad is in this post. If you do want an explanation of what a monad is, I recommend the marvels of monads, an old post that made the concept “click” for me. Querying your Monad Language integrated query (LINQ) was a major feature introduced way back in C#3. // before linqvar transformedList = new List();foreach (var item in inputList) { transformedList.Add(item * 2 + 1);}// after linqvar transformedSequence = from item in inputList select item * 2 + 1; You probably already know about query expressions, but you may not have known that you can query over more than just lists and other enumerables. Another common type of thing to make queryable is nullable values.

Abstraction, intuition, and the “monad tutorial fallacy” | blog :: Brent -> [String] 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! Unfortunately, there is a whole cottage industry of monad tutorials that get this wrong.

The Lambda Calculus First published Wed Dec 12, 2012; substantive revision Fri Feb 8, 2013 The λ-calculus is, at heart, a simple notation for functions and application. The main ideas are applying a function to an argument and forming functions by abstraction. 1. The λ-calculus is an elegant notation for working with applications of functions to arguments. λx[x2 − 2·x + 5]. The λ operators allows us to abstract over x. The first step of this calculation, plugging in ‘2’ for occurrences of x in the expression ‘x2 − 2·x + 5’, is the passage from an abstraction term to another term by the operation of substitution. This example suggests the central principle of the λ-calculus, called β-reduction: (β) (λx[M])N ⊳ M[x := N] The understanding is that we can reduce or contract (⊳) an application (λxM)N of an abstraction term (the left-hand side, λxM) to something (the right-hand side, N) by simply plugging in N for the occurrences of x inside M (that's what the notation ‘M[x := N]’ expresses). 1.2 Intensionality 2.

concrete monads 1 : Maybe, Either, list, IO Writing a Raytracer in Common Lisp - Part 1 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. 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. λ> simpleHTTP (getRequest url) >>= getResponseBody >>= putStrLn {"took":11,"timed_out":false ...

Reusing Thunks for Recursive Data Structures in Lazy Functional Programs - Yasunao TAKANO 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. In other words, if you only read those posts and nothing else about monads, you probably wouldn't be able to use monads in real code. This was intentional because I felt that the practical stuff (like do notation) had adequate treatment in other resources. In this post I'm still not going to talk about the details of do notation--you should definitely read about that elsewhere--but I am going to talk about some of the most common things I have seen beginners struggle with and give you cookbook-style patterns that you can use to solve these issues. Problem: Getting at the pure value inside the monad This is perhaps the most common problem for Haskell newcomers. main = do lineList <- lines $ readFile "myfile.txt" -- ... do something with lineList here That code generates the following error from GHC: Both of these functions are defined in Prelude.

Introduction This blog will follow my steps toward learning F#. I am hoping to use F# and XNA to write small games for the XBox 360. I have no idea yet on the feasibility of that, but if it works, it would open a whole world of fun opportunities. I would normally not write a tutorial about something I don't know, but seeing the current lack of tutorials for F#, it might be beneficial for others to follow my progress. I will start with a series of tutorials dedicated to implementing a Runge-Kutta integrator. I will not explain how to install and set up an F# development environment, that information is available on the net.

How I learned to understand Monads : haskell Why Do Monads Matter? « Sententia cdsmithus (A Side Note: I’ve been formulating the final thoughts on this post for about a week now. 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? If you’re a software developer, have you heard about monads and wondered what they were? Or if you’re interested in mathematics, have you heard murmurs in the past about how category theory interests computer science people? These are the kinds of questions I begin with. Ready? Computer Programming and Functions: A Tenuous Relationship Quick quiz: Do computer programmers use functions? The truth, though, is a bit more complicated. Oh no…

Related: