15Jan

Coffeescript is a gateway drug to Haskell

Posted by Elf Sternberg as javascript

So, I’ve been working my way through Learn You A Haskell For Great Good, and in chapter five there’s a quicksort example that reads:

quicksort [] = []
quicksort (x:xs) =
           let smallerOrEqual = [a | a <- xs, a <= x]
               larger = [a | a <- xs, a > x]
           in quicksort smallerOrEqual ++ [x] ++ quicksort larger

I wondered what it would be like in Coffeescript. Frighteningly enough, other than moving the guard condition into the function itself, they look remarkably similar:

quicksort = (x) ->
    return [] if x.length == 0
    h = x.pop()
    smallerOrEqual = (a for a in x when a <= h)
    larger = (a for a in x when a > h)
    (quicksort smallerOrEqual).concat([h]).concat(quicksort larger)

Coffescript is a gateway drug to Haskell. If you don’t want to learn Haskell, turn back now. (On the other hand, I think learning Haskell will be a hell of a good thing for me. My brain needs the exercise.)

2 Responses to Coffeescript is a gateway drug to Haskell

Randall Leeds

January 26th, 2012 at 3:03 am

Hahahaha. Indeed. I got the same curiosity about the bind operator on monads. Since I couldn’t make it look pretty enough in CoffeeScript I found Coco (https://github.com/satyr/coco), which has a <- operator for flattening callbacks. I got carried away and a few days later had this: https://github.com/tilgovi/skuld/blob/master/src/monad.co#L134

So much for building a Paxos framework.

Elf Sternberg

January 26th, 2012 at 8:51 am

Good grief, that’s cool! I’m still a little married to Coffeescript, professionally, at the moment, but really, I’m still trying to master Monads, and that makes it pretty damn clear what’s going on.

Comment Form

Subscribe to Feed

Categories

Calendar

January 2012
M T W T F S S
« Dec   Feb »
 1
2345678
9101112131415
16171819202122
23242526272829
3031