Category Archives: Haskell

Getting Started with Haskell – Comprehending List Comprehensions

As promised, we are now in the Season of Haskell.  I decided to spend this week with some baby steps and also try to figure out what resources to use to get started.  I had heard good things about Learn You a Haskell For Great Good and Real World Haskell.  I felt like reading the first few chapters of each would give me a feel for the book styles and let me narrow my focus.

I have to say that there are certainly some neat things that Haskell can do in a fairly terse syntax. List comprehensions are pretty cool. There is an example in Learn You a Haskell that allows you to find all the right triangles with a hypotenuse less than 10 in one (fairly) simple line, once you start to grok the syntax.

[ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2] 

What this says is get me the list of points, actually tuples, (a,b,c) where

  • c is drawn from the range 1..10
  • b is drawn from a range of 1..c
  • a is drawn from a range of 1..b
  • and the predicate function a^2 + b^2 == c^2 is applied to test that it is a right triangle

The result is [(3,4,5),(6,8,10)]

Versus something like this in Ruby

rightTriangles = []
(1..10).each do |c|
  (1..c).each do |b|
    (1..b).each do |a|
      if (a**2 + b**2 == c**2)
        rightTriangles << [a,b,c]

Obviously Ruby has a lot of other strengths going for it, but clearly this is the type of problem where functional languages may shine. And who knows, there may be ways to bring some of these ideas into my Ruby programming. Might be a good time to re-watch Pat Shaughnessy’s talk on Functional Programming and Ruby.

I’m curious to see what comes next.


It’s Time To Go Functional

It was 3 years ago that I embarked on a learning adventure with Ruby and Rails, my Season of Ruby, that would change my career in ways that I could not imagine at the time. The Pragmatic Programmer recommends learning one new language a year, and though I’ve added a few languages and frameworks since then, Javascript and AngularJS to name a few, I’ve never learned a purely functional language.

My experience in programming to this point has been procedural (way back in C and assembly) and object-oriented (C++, C#, Java, Ruby).  I have no experience in functional programming, and I wonder if that leaves a gap in the tools I have in approaching a problem.  I have no idea where this will lead, or how this might change me as a developer, but that’s fine.  It worked out ok last time with Ruby.

My motivation behind this is strictly to learn a new way to think about and solve problems, not find a shiny new language/framework to land a new gig.  For that reason I am planning on learning a pure functional language, most likely Haskell. Having talked to a few people there are other options out there, but Haskell keeps coming up as the top choice if you want to learn the concepts of functional programming.

I’m planning on starting out with Learn You A Haskell For Great Good.  It is a resource that has popped up in discussions and threads as this idea has festered in my brain, but if folks have other ideas I’d love to hear them.

Stay tuned, someday soon I may actually understand what a monad is.