RSS

Functional Programming and Haskell

14 Feb

Hey everyone — sorry it’s been a while since my last post.  School got off to a crazy start, and I’ve been really busy planning a hackathon, writing material for my iOS course, and just generally being involved with life on campus.  Oh right, and I am taking a few CS courses, too.

This post in particular is dedicated to a hidden gem that I found, and one that has given me a new way to look at programming and problem solving:

In addition to taking my two core CS courses, Data Structures and Algorithms, I enrolled in a a linguistics course that explores how we can represent and manipulate meaning in a computer.  In order to do that, we are learning Haskell. It is an incredible language. Here’s where I’ve been learning, and here’s the scoop:

It’s functional. What does that mean? I had learned Scheme for my introduction to CS course, but I did not understand the essence of functional programing. It’s based on lambda calculus, which is a system for manipulating functions.  Essentially, the lambda “term” are defined as follows (you can read more here if you’re interested):

  1. Any variable or atomic statement, x
  2. Abstraction, λx . M – i.e. turn M into a function, with x as the variable
  3. Application, (M N) – i.e. replace every appearance of x in M (see 2) with N

It may seem a bit confusing, but it is quite similar to how we think about math.  You have functions, denoted f(x) or the like, and you can apply that function to x.  Alternatively, given some expression (without an equal sign), you can turn it into a function by setting it equal to g(x) or the like.  The single most powerful feature of lambda calculus is the ability to pass a function as an argument to another function.  Just like we can apply a function to a variable or a constant (1), we can also do something like f(g(x)) and get a totally new function which we can then apply to some other expression.  What’s more, and here is a place lambda calculus gets cool, is that if a function takes multiple variables, if we give it a value for one of the variables, it becomes a totally new, and perfectly legal function.  This has tremendous implications for how we can manipulate data, and gives you the opportunity to write highly flexible code.

But enough of my raving.  If you don’t believe me, read thisthis, or this.  And get started learning Haskell!  Here are my favorite things about the language:

  • Strong typing – the compiler will yell at you if your types don’t match exactly.  Seriously.  And ghc (glasgow haskell compiler) will give you very precise, useful error messages.  This prevents a lot of runtime errors, and usually means that you will have a working program once it has successfully compiled.
  • First class functions – since Haskell is functional, there is no concept of “parameters” per se, only terms to which you are applying a function.  Because of this, functions are applied to terms immediately following them, and function application takes the highest precedence.  Say goodbye to parentheses!
  • First class math – so many built in functions for math.  One line fibonacci sequence. ‘Nuff said.  Maybe not – the use of guards instead of if-else statements makes code look like real, pencil-and-paper math
  • Infinite lists – didn’t think that was possible?  Thank laziness.

Sold?  If you’re interested in learning the language, I would start with Learn You a Haskell, which gives a great rundown of all the important library functions, as well as gives you a good sense of how to use the functional constructs of Haskell.  More advanced programmers may want to look here.

About these ads
 
Leave a comment

Posted by on February 14, 2012 in General, Programming

 

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: