Sunday, August 5, 2012

My first real code golf program!

I've always been interested in Code golf, which essentially boils down to creating the shortest possible program that completes some task.

I finally got around to completing one of these challenges, and though I wasn't really that close to winning, it was fun to mess around with it it was one of the shorter Haskell solutions!

So, the challenge I attempted was Reddit's Tiny Code Challenge #4, asking for the shortest program that finds the longest identical subsequence in a string.

Directly from the challenge page:
For example:
aaabbbbccc aabccdcdd -> aab, bcc
adeaabbbbccc aabccdedcdd -> aab, bcc
abcdef ghijk -> nothing
abcdef fghijk -> f
So, here was my solution in Haskell:

Let's break this down into steps, since it probably looks like a bunch of mumbo-jumbo!

And there you have it! 193 characters of Haskell code got us a pretty cool program.

Here's a little bit of output: 
>tc04.exe "aaabbbbccc" "aabccdcdd"
>tc04.exe "abcdef" "ghijk"
>tc04.exe "abcdef" "fghijk"
>tc04.exe "fhqwhgads" "where are all the dragons?"
Cool! :)


No comments:

Post a Comment