background preloader

Functional Programming For The Rest of Us

Functional Programming For The Rest of Us

Introduction Haskell is a computer programming language. In particular, it is a polymorphically statically typed, lazy, purely functional language, quite different from most other programming languages. The language is named for Haskell Brooks Curry, whose work in mathematical logic serves as a foundation for functional languages. Haskell is based on the lambda calculus, hence the lambda we use as a logo. 1 Why use Haskell? Writing large software systems that work is difficult and expensive. WOW! Even if you are not in a position to use Haskell in your programming projects, learning Haskell can make you a better programmer in any language. I learned Haskell a couple of years ago, having previously programmed in Python and (many) other languages. Haskell offers you: Substantially increased programmer productivity (Ericsson measured an improvement factor of between 9 and 25 using Erlang, a functional programming language similar to Haskell, in one set of experiments on telephony software). 3.4 Brevity

Did it with .NET - A Higher Calling After a long break, it's time to return to my informal series of articles on functional programming concepts using only C# 2.0. While C# 3.0 might be syntactically more suitable to functional-style programming, 2.0 has the advantage of being more familiar, and in some cases, clearer. In addition, using C# 2.0 makes compiling sample code under Visual Studio 2005 possible, so an installation of the Orcas beta (i.e. Visual Studio 2008) is unnecessary. Because this article builds on information that I have already been presented, a refresher might be in order. Here are links to help you get your bearings: Let's get started... Higher-Order Functions The notion of higher-order functions is an amazingly simple (and extraordinarily powerful!) In order to support higher-order functions, a language must treat functions as first-class objects, but what does that mean? In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. The Big Three Map

Excellent Overview of Functional Programming 14 Feb 2008 I just ran into an article on DZone that totally threw me off my chair in terms of content. It’s titled (ambitiously enough) “Functional Programming For the Rest of Us“. Basically the article attempts to explain many of the common FP techniques in terms that imperative programmers can understand. More importantly, it motivates these explanations with a long and rambling set of real-world reasons. It’s extremely long, extremely involved and well worth the read. It’s worth noting that the author claims to be presenting his explanations in Java, but is in fact devising an extension of Java to represent FP constructs more concisely. The really interesting bit is how the author motivates FP through a combination of real-world examples and lengthy reasoning. So grab yourself a cup of coffee and a box of donuts (it’ll take you about that long to finish the article), and slog your way through.

Eric White's Blog : Query Composition using Functional Prog Functional Programming (FP) has the potential to reduce program line count by 20% to 50%, reduce bugs and increase robustness, and move us in the direction of taking advantage of multiple core CPUs. But perhaps due to the learning curve, and unfamiliarity, many developers are not taking advantage of the potential that LINQ and FP offers. This blog is inactive.New blog: EricWhite.com/blogBlog TOCThis is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries. It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need to understand these techniques. Learning about functional programming made me a better and faster coder. The tutorial is written in a granular fashion so that if you already understand a topic, you can skip it. 1. Presents the case for why you want to learn functional programming. 2. Functional programming is most applicable when transforming data. 3.

Can Your Programming Language Do This? by Joel Spolsky Tuesday, August 01, 2006 One day, you're browsing through your code, and you notice two big blocks that look almost exactly the same. In fact, they're exactly the same, except that one block refers to "Spaghetti" and one block refers to "Chocolate Moose." // A trivial example: alert("I'd like some Spaghetti!") These examples happen to be in JavaScript, but even if you don't know JavaScript, you should be able to follow along. The repeated code looks wrong, of course, so you create a function: function SwedishChef( food ) { alert("I'd like some " + food + "!") OK, it's a trivial example, but you can imagine a more substantial example. Now you notice two other blocks of code which look almost the same, except that one of them keeps calling this function called BoomBoom and the other one keeps calling this function called PutInPot. alert("get the lobster"); PutInPot("lobster"); PutInPot("water"); alert("get the chicken"); BoomBoom("chicken"); BoomBoom("coconut"); Look! Ok. Next:

Functional Programming in C# 3. As a developer who was raised on procedural and object oriented programming languages like C, C++ and Java it took me a while to figure out what people were raving about when it comes to the benefits of functional programming techniques. I always thought closures and higher order functions were words used by snobby kids from MIT and grad students to show how overeducated they were as opposed to programming tools I'd ever find useful. This thinking was additionally fueled by articles like Joel Spolsky's Can Your Programming Language Do This? which not only came off as snobby but also cemented the impression that higher order functions like map() and reduce() are for people solving "big" problems like the folks at Google who are trying to categorize the entire World Wide Web not people like me who write desktop feed readers in their free time. All of this changed when I started learning Python. There are more examples from the RSS Bandit code base but I'm sure you get the idea.

The Scala Programming Language F# 101 riginally a research language from Microsoft Research, F# has long been a "secret weapon" in the arsenal of .NET programmers for doing statistically and mathematically heavy coding. More recently, however, a growing number of developers have begun to see the inherent advantages implicit in writing functional code, particularly in the areas of concurrency. The buzz has begun to approach interesting levels following on the heels of an announcement last year from the head of the Microsoft Developer Division, S. "Soma" Somasegar, that F# would be "productized" and fully supported by Microsoft as a whole, suddenly removing the stigma and licensing restrictions that surround research projects. Concepts F# is a CLR-adapted version of the Object-CAML language, also known as OCaml. Before going too deeply into a more conceptual discussion, a quick glance at some F# code may help put a framework around some of these ideas. Setup To get started, you must install the F# binaries. Congratulations!

Roundup: Scala for Java Refugees 13 Feb 2008 To be honest, I’m somewhat kicking myself for writing this post. As I’ve said many times: roundup posts are for people who are too lazy to write real content. I can’t tell you how many blogs I’ve come across which have a roundup-to-post ratio of easily 3:1. You know it’s a bad sign when sites start having roundups of their roundups… Meta-roundups aside, I decided (after much deliberation) that a single post linking to all six parts of the series would be useful to one or two people. Of course it’s always possible that you subscribe to my way of looking at things. Part 1: main(String[]) Introductory article giving motivation for learning Scala and some first steps to “get your feet wet” in the language. Part 2: Basic OOP Looking at Scala’s syntax in a little more detail. Part 3: Methods and Statics Scala’s method syntax is far too powerful to cover in a single post. Part 4: Pattern Matching and Exceptions Part 5: Traits and Types Part 6: Getting Over Java

Why Scala? Before answering the question of "Why Scala?", we first need to answer the question "what is Scala?" From the Scala website, the following overview can be found: Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages. For some developers, these incentives will be enough to lure you away from Java into the Scala world. So, once again, "why Scala?" In a post titled "Scala: The best of both Ruby and Java", Ian describes that it might not be a choice between Java and Scala, but instead the choice to use a combination of Java and Scala as an alternative to other language choices such as Ruby: Many programmers love Ruby, they just can’t get enough of it. Continuing on, Ian notes that: This opinion is shared by David MacIver in the blog posting No, seriously, why Scala? Scala's far from perfect.

Related: