View RSS Feed

ZekeDragon

I'm a Fan of the Functional

Rate this Entry
by , 03-17-2010 at 09:53 AM (950 Views)
I've really started to like functional programming, not because I can yet produce a great deal (though I can write some pretty detailed command line programs), but because it is extremely rewarding to accomplish what I want to do with it. There's a certain kind of spark that comes from functional programming that just isn't available in imperative languages... it's what I feel from Python when I come up with a particularly succinct and beautiful algorithm, except nearly every time I write a functional program. Consider a common programming challenge for newbie programmers, writing one of these pyramids:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

This is easy in a language with loops, but it's particularly beautiful in a functional language. Consider this Java code:
Code:
public static String templeString(int size)
{
    String retstr = "";
    for (int i = 1; i <= size; ++i)
    {
        for (int j = 1; j <= i; ++j)
        {
            retstr += (j + " ");
        }
        retstr += "\n";
    }
    return retstr;
}
Not bad, really. Now see the Haskell version:
Code:
templeString = makeRetStr 1 1
    where makeRetStr x y s
              | y > s     = []
              | x > y     = "\n" ++ makeRetStr 1 (y + 1) s
              | otherwise = show x ++ " " ++ makeRetStr (x + 1) y s
The Java code will also, of course, require a surrounding class and a method to read the user input properly, but for the sake of simplicity I'll just make it use parseInt:
Code:
public class Test
{
    public static void main(String[] args)
    {
        if (args.length > 0)
        {
            System.out.println(templeString(Integer.parseInt(args[0])));
        }
    }
    // ...
}
Doing the same for Haskell yields this:
Code:
import System.Environment

main = getArgs >>= (\(x:xs) -> putStrLn $ templeString $ read x)
With a quick test of both these programs, it's pretty simple to see that they both function properly:
Code:
zekedragon@Princess-Flowercakes:~/Desktop/test$ ./htest 5
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

zekedragon@Princess-Flowercakes:~/Desktop/test$ java Test 5
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5
So, given this example, what exactly makes the Haskell version in any way superior to the Java version, or to any according C, C++, Perl, Python, etc. version?

What if I told you the Haskell version wasn't even that good? That we could do better? What if I told you that this was a one-liner?
Code:
templeString n = foldl1 (++) [foldl1 (++) [show x ++ " " | x <- [1..y]] ++ "\n" | y <- [1..n]]
That's some good Haskell... and I believe that this is what is really meant by "expressive power". You can express a solution to nearly any problem with only a few keystrokes, all that's needed is a mind to break down a problem into a logical series of actions on code rather than a logical series of interactions between objects. I would never say that OOP is bad, but I think that functional may just be better.

Anyway, yeah, I've been really digging this language, because there really is so much that you can do with it. Since I've been getting into these languages, I've started a group on CC for Functional programming, called Fans of the Functional, and if you like functional programming too you should join! Yay advertising!

Yeah, other than that my life has been crazy. Every day I've been solving problems in my life, like getting steady employment, paying bills, going to college, getting my license back, new computer stuff, etc. It's good, but not that important for here. XD

Submit "I'm a Fan of the Functional" to Digg Submit "I'm a Fan of the Functional" to del.icio.us Submit "I'm a Fan of the Functional" to StumbleUpon Submit "I'm a Fan of the Functional" to Google

Tags: None Add / Edit Tags
Categories
Programming

Comments

  1. DarkLordoftheMonkeys's Avatar
    I've always wanted to learn functional programming. I was introduced to it by a book I read on programming languages last December, and it looked really intriguing. It seems like it would be a good intellectual challenge, which for a lot of us is what makes programming so enjoyable. I'm hoping there is a GNU Common Lisp package that works on Mac, and doesn't require you to compile it from source (which for me always results in compiler errors).
  2. ZekeDragon's Avatar
    You should get into it, in my opinion. A cursory Googling yielded this blog post, but I haven't looked terribly deep into it. I know you can get GHC for Haskell here.