Thursday, December 24, 2009

yield in Ruby took a while to sink in - Today, I used it for an implementation and thats when I understood it well - For the new year greeting program - to send automatic customized emails.

I had to send customized inline images in the email - for that I had to write an Image manipulation program. I used Ruby for that (Only because I am not comfortable with Haskell yet!) git://

So the Image class contains [] operators etc but for any function to be applied on the image, you typically need to go over each pixel and change it in some manner. Also, if the color value went out of the range (less than 0 or greater than 255), it had to be brought back into the range. I found myself uninterestingly repeating the act of traversing over the pixels and ensuring that the resulting value stayed in the range. That's when it occurred to me that I could abstract it out into the image class into a function called - apply function! - it looks like this

def applyFunction
@width.times do |x|
@height.times do |y|
(r,g,b)=(yield x,y,r,g,b).map! {|c| Image.normalizeColor c}

Monday, July 06, 2009

Okay ... I got a little confused by working on list of lists!

so it turns out that f.g = \x -> f (g x)

so, = \f -> map (map f)

( (+1) list = (\f -> map (map f)) (+1) list = map (map (+1)) list
Everything seems already done!

I always found writing GUI code boring - always involved boilerplate code - be it, X windows, Windows, AWT ....

So, I was thinking of writing some automation in Haskell that could generate Swing code - looks like SwingML does just that - not in Haskell though.

Friday, June 05, 2009

Quick Web development with Apache and Haskell (with a slight touch of Perl)

I started learning Haskell a few months ago. It has been a personality changing
experience! Because of the blub paradox - Unfortunately, no one can be told what the Haskell Experience is. You have to feel it for yourself.

Haskell has been the most difficult computer language that I've learnt so far (C,x86 ASM, Perl, Ruby, Java) - From what I understand, my long term exposure to those imperative languages is one of the reasons why I find it difficult.

I am sure, there are many out there who have just begun their Haskell journey. So, here's a quick way in which you could develop Hakell programs that do something interesting - Web development.

Okay, I assume, you know how to setup Perl CGI in your apache web server. It works by default on the Apache installation on my RHEL5 Laptop. The idea is to use a small bridge CGI script in Perl that executes standalone Haskell files and pipes in the CGI parameters into the haskell program and gets back whatever the haskell program writes to its stdout and delivers it back to the web client.

Here's the bridge perl program -


use CGI qw/:standard/;

print header;

$q=new CGI;
%params = $q->Vars;


use FileHandle;
use IPC::Open2;

$pid = open2(*Reader, *Writer, "./$programName" );
for(keys %params){
print Writer "$_ => ".$params{$_}."\n";
close Writer;

@got = <Reader>;
print @got;

So, this bridge simply looks for the program name in the request parameters and then executes it with handles to it's stdin and stdout.

The program to be executed could be a regular Haskell program like this one -

import IO

main = do
getContents <- hGetContents stdin putStrLn ("You entered the following " ++ getContents) =====================