Learning Haskel For Fun (and profit?)

I found a little spare time recently, so I started learning Haskell via LearnYouAHaskell.com. I’m learning it specifically because it’s completely foreign to me. I’ve never done any functional programming and I wanted to bend my mind around and see what I could learn from that paradigm. So I asked what I should learn on twitter, and that was the overwhelming recommendation.

It’s been fun so far. I’ve only completed the “starting out” section – the very section of writing code, but already there are several things that I can do in Haskel that are making me see other languages a little differently.

List Comprehensions

They’re basically the same thing as “select” and “map” from Ruby or from Underscore.js in JavaScript. And yes, CoffeeScript supports them directly, too. But they are much more compact and supported directly by the Haskell language instead of as functions in a library or pre-compiler. There’s also more too them than just select/map, but I still don’t quite get it all.

Here’s a list comprehension in Haskell, borrowed from LearnYouAHaskell:

[x*2 | x <- [1..10]]  
--> [2,4,6,8,10,12,14,16,18,20]

Here’s the same thing in Ruby:

(1..10).map{|i| i*2}
#=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

(I know you can do list comprehensions in CoffeeScript, too, but I don’t know CoffeeScript yet.)

Tuples

I never understood the point of tuples in .NET. But after seeing them in Haskell, it is starting to make sense – at least in Haskell.

A tuple is an anonymous type, defined at runtime, where the tuple’s type is built from the types of the individual pieces: Tuple<T1, T2, T3>, as an example in .NET terms. The semantics of a Tuple are that it must be the same number of types in the tuple and the same types of each item in the tuple, to be considered equal from a type perspective.

This means that Tuple<int, string> is the same type as Tuple<int, string> in .NET, but is different from Tuple<string, int>. I think I can see some uses for this to provide anonymous types and equality checks amongst them, in .NET – however, it seems to make more sense to me in Haskell where I can assign a tuple at runtime, building up an object type that never existed before, as an anonymous type.

Here’s a tuple in Haskell:

(42, "meaning of life")

And the same thing in .NET 4:

var tuple = new Tuple<int, string>(42, "meaning of life")

Both of these create a tuple of type <int, string> (and both of these are statically typed languages). I’m still not 100% sure I see the point of a tuple in .NET since we have Expando and Dynamic… but there must be a use case for a semantically equivalent, anonymous type in .NET, somewhere. I’ve not run into it, but I know other people use the Tuple type in .NET.

I’m also not sure I can show a tuple in Ruby or JavaScript. Anyone know how to do this, and keep the same semantics of type equality?

Other Great Things, Too

Whether or not I ever use Haskel on a project (highly unlikely), the experience will at least help me see different pieces of the other languages I use, in a new light. If you haven’t done any functional programming, check it out. It’s a mind-bender for sure.

But What Do People Build With Haskell?

No, really… what do people write with Haskell? Is it used in real systems? Where? What types of applications and systems? I’d love to know more about it’s real-world uses.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in C#, Haskell, Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Julian Birch

    Checkout Tsuru Capital and http://bos.github.com/strange-loop-2011/talk/talk.html#(1)

    • http://mutedsolutions.com Derick Bailey

      nice! thanks for the tip :)

  • http://twitter.com/pioners1001 Maris Krivtezs
  • http://realfiction.net Frank Quednau

    Some time ago I also delved into Haskell from a .NET dev perspective. I documented quite a few bits at my site: http://realfiction.net/tag/Haskell

    As to where Haskell is used in real stuff out there, I also don’t have an answer…

  • Omer Mor

    It’s easier to write Tuple.Create(42, “meaning of life”) and let the compiler infer the types.

    • http://mutedsolutions.com Derick Bailey

      Yeah, that’s quite a bit more clean. I didn’t know about that option, thanks!

      • Julian Birch

        I think the problem is that tuples are more useful if you can destructure them and not have to declare their parameter types all of the time.

  • lefthandedgoat

    (42, “meaning of life”) is the syntax for a tuple in F#