background preloader

Haskell

Facebook Twitter

Types

Pearls. Simon Peyton Jones: papers. 2008-10-29/kestrel.markdown at master from raganwald's homo. Much Ado About Monads – List Edition - Matthew Podwysocki. In the previous installment in my quest to make monads a little more approachable, I covered the maybe monad and how it can apply as a general design pattern for dealing with lookups that may or may not succeed. We covered how as a functional design pattern, we can use these monads to easily compose functions together in such a way that makes sense. This time, we’ll look at the list monad, and how you may already be using it without knowing it. What’s the Problem? In order to understand the list monad, we first need to understand the problem it’s trying to solve. In our last example with the maybe, we wanted to represent whether we had a value or not. This time, we might want to return any number of results that we don’t know ahead of time. Taking an example from the Real World Haskell book, let’s take an example of trying to find all combinations of numbers that when multiplied together, equal a given number.

Return list; } The List Monad ghci> multiplyTo 45 [(1,45),(3,15),(5,9)] The Fun of Programming. Some of the code and software from the book is available from the links below. Please note that the software versions will be those from around the time of publication of the book and are provided here for the convenience of those wishing to "dip into" the chapters or use a version known to work with the examples in them. If you want to use any of these pieces of software seriously, you are strongly encouraged to obtain the latest version from the appropriate webpage, linked below. Answers to the exercises for some of the chapters are available, and will be released to bona fide instructors. Currently this only includes chapters 3 and 5. Chapter 1, Fun with binary heap trees, Chris Okasaki Code from the chapter, all Haskell 98 compliant: Binary heap trees, Maxiphobic heaps, Round-robin heaps, Skew heaps Chapter 2, Specification-based testing with QuickCheck, Koen Claessen and John Hughes QuickCheck: gzipped tar archive, zip file.

The QuickCheck homepage The Haskore homepage The MAG homepage. Brian Beckman: The Zen of Stateless State - The State Monad - Pa. Concurrency is a problem that faces all developers as we move to the age of ManyCore processor architectures. Managing state is an important aspect of programming generally and for parallel programming especially. The great Brian Beckman demonstrates three ways of labeling a binary tree with unique integer node numbers: (1) by hand, (2) non-monadically, but functionally, by threading an updating counter state variable through function arguments, and (3) monadically, by using a partially generalized state-monad implementation to handle the threading via composition.

Of course during this lesson from one of the masters of mathematical programming, we wind through various conversational contexts, but always stay true to the default topic in a stateful monadic way (watch/listen to this piece to understand what this actually means This is another great conversation with astrophysicist and programming master Brian Beckman. Exercise 4 (HARD): go from binary tree to n-ary tree. Design patterns in functional languages. Good explanantion of monads. Monads - Another way to abstract computations in Scala. You can view monads as containers or as computations. Isn't this confusing enough ? To a programmer, the biggest scare with the name "monad" is the fact that it originates from the constructs of category theory and the rich mathematics that goes along with it.

The simplest way to make an idea of monads is to look at applications and examples that use them to solve real life problems. Monads have been described as models of computation, code transformers, state transformers and what not. But unfortunately for someone uninitiated to monadic programming, and indoctrinated with the tenets of OO programming and design patterns, all these incarnations make little sense. I have been playing around with Scala for some time now. This post is all about my exposure to monads in Scala. This sequencing and threading of actions by a monad is done by the language compiler that does the transformation through the magic of closures. For { x <- List(1, 2)}yield(x + 2) List(1, 2) map {x => x + 2} Monads as computation. 1 Motivation Programmers in general, and functional programmers in particular, are usually not so content to solve a problem in a fragile way by coding a solution directly.

Quite often the best way to solve a problem is to design a domain-specific language in which the solution to one's problem is easily expressed. Doing this generally ensures that a wide class of similar problems can be attacked using the same code. That way, you get code which is resistant to damage in the form of changes to design requirements. Better still, we'd like to embed those domain specific languages into the language which we wrote them in, so that they can be used together, and so we get benefits from the language we're working in without so much extra work.

So we write combinator libraries which are essentially libraries of code whose API's are sufficiently powerful that using the library is like programming in a small language embedded within the existing one. 2 The parts of a monad 3 A few examples . Or: Monads as containers. Functor. Quasiquoting Support for GHC. Quasiquoting is supported in GHC 6.10 and above. See the Quasiquotation tutorial on the Haskell wiki for a simple example. Also, please note that the syntax has changed slightly from that which is presented in the paper.

Audrey Tang has backported this feature to GHC 6.8.3. Her patch is available here . Quasiquoting Publications Why It's Nice to be Quoted: Quasiquoting for Haskell . A C Quasiquoting Library The language-c-quite library for quasiquoting C and variations, including OpenCL and partial support for CUDA, is available on hackage and on github . Learn You a Haskell for Great Good! - Starting Out. Ready, set, go! Alright, let's get started! If you're the sort of horrible person who doesn't read introductions to things and you skipped it, you might want to read the last section in the introduction anyway because it explains what you need to follow this tutorial and how we're going to load functions. The first thing we're going to do is run ghc's interactive mode and call some function to get a very basic feel for haskell.

Open your terminal and type in ghci. GHCi, version 6.8.2: :? GHCi, version 6.8.2: :? Congratulations, you're in GHCI! Here's some simple arithmetic. ghci> 2 + 15 17 ghci> 49 * 100 4900 ghci> 1892 - 1472 420 ghci> 5 / 2 2.5 ghci> ghci> 2 + 15 17 ghci> 49 * 100 4900 ghci> 1892 - 1472 420 ghci> 5 / 2 2.5 ghci> This is pretty self-explanatory. Ghci> (50 * 100) - 4999 1 ghci> 50 * 100 - 4999 1 ghci> 50 * (100 - 4999) -244950 ghci> (50 * 100) - 4999 1 ghci> 50 * 100 - 4999 1 ghci> 50 * (100 - 4999) -244950 Yikes!

Blog.bjrn.se: Speeding up Haskell with C – a very short introduc. You are working on your project in Haskell. You notice the performance is not as great as it could be. Here’s what you can do, apart from writing more efficient Haskell code. Profiling First, of course, you have to profile your code to know what’s really slow and not. Using GHC, compile your program with -prof -auto-all. $ ghc -O2 --make Project.hs -prof -auto-all Doesn’t work? $ cd /to/the/library/source $ runghc Setup.hs configure --enable-library-profiling $ runghc Setup.hs build $ runghc Setup.hs install Don’t worry, this will not mess up the current installation, it will just add a special profiling version of the library next to the regular version.

You can now test your code. $ Project +RTS -p The GHC profiler is competent and can profile both time and space. The output is a file, Project.prof. Dctiv :: Int -> [Double] -> [Double]dctiv points input = ... This is probably a typical function you want to write in a low level language. Let’s write C! Dctiv.h dctiv.c Build an object file: Functional data structures okasaki.