Matrix Multiplication in Haskell

by

in

As I mentioned in a previous post, I’m trying to learn Haskell.  I’m not progressing super quickly because it’s not a terribly high priority, but it’s sort of fun to think about programming problems in a totally different way.  While Haskell (being a functional language) definitely fits this niche, the book that I’m following does not include any exercises — you can sort of work the examples ahead of time and compare your solution to the book’s, but that’s a poor substitute.

I was helping a friend of mine (a sophomore in computer science) with an assignment he was working on recently, when it struck me that the straightforward, simple assignments he’s doing might make good projects to tackle in Haskell.  The course he’s in is a data structures course, so a lot of the projects have fairly simple input / output specifications, and somewhat complex backend requirements.  I chose to ignore the data structure parts (I’ll save implementing stacks / linked lists / etc. in Haskell for another day) and tried to tackle just the functionality.  The first (and so far, only) project I worked on reads in two matrices and either multiplies them or informs the user that the matrices’ sizes are incompatible.  The multiplication is straightforward — I’m not using the Strassen algorithm — and the input / output formats are easy as well.

The project turned out to be both more difficult and more instructive than I had planned.  Some observations I made while writing my program:

  • Parsing in a functional language is totally foreign to me.  The math wasn’t all that hard to grasp (though it wasn’t exactly straightforward either) but doing simple things like splitting a line into chunks (“tokens”) based on some delimiter is a completely new experience in a functional language.
  • Nearly all my methods have essentially one line of functionality.  I’m not sure if this is how Haskell is “supposed” to be written, or if it shows that I’m doing something wrong.  Either way, the length of functions is something I’m definitely going to pay more attention to when I look at examples in the future.
  • I’m not very good at list comprehensions.  Below are two ways of multiplying matrices: First, my way:

    Second, the way it’s implemented on rosettacode.org:

All in all, I learned a lot.  It’s some pretty ugly code, and I may rewrite it further, or tackle a different project from the same class and try to go about it in a better way.  If you’d like to look at my code or compile / run it (with the understanding this is just sort of doodling-type-code and isn’t documented or optimized or very good) feel free to download it.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.