I'm a Fan of the Functional
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:
Not bad, really. Now see the Haskell version: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; }
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: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
Doing the same for Haskell yields this:Code:public class Test { public static void main(String[] args) { if (args.length > 0) { System.out.println(templeString(Integer.parseInt(args[0]))); } } // ... }
With a quick test of both these programs, it's pretty simple to see that they both function properly:Code:import System.Environment main = getArgs >>= (\(x:xs) -> putStrLn $ templeString $ read x)
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?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
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?
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.Code:templeString n = foldl1 (++) [foldl1 (++) [show x ++ " " | x <- [1..y]] ++ "\n" | y <- [1..n]]
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










