If you work on large-scale development projects, then you’re familiar with the advantages of writing modular code. Well-structured, modular code is easier to write, debug, understand, and reuse. The problem for Java developers is that the functional programming paradigm has long been implemented only via specialized languages such as Haskell, Scheme, Erlang, and Lisp. This article, shows you how to use functional programming constructs such as closures and higher order functions to write well-structured, modular code in the Java language.
I use some of those techniques, and I love how I can avoid writting tedious code, but that code tends to be harder to understand if you are not familiar with those constructs.
First, I have to say that I’m getting a little tired of people who are enamored of a particular programming paradigm implying that you can’t write well-structured, modular code without it. I see it in OOP and functional fans in particular. Speaking as a fan of both and more, I still find it annoying. You can write well-structured, modular code using any number of different paradigms.
Whether the language has direct support for them or not, as this article illustrates rather well. Still, it all seemed awkward. It made me happy that there are other languages for the JVM that do a better job with functional code – you aren’t stuck with Java.
The situation is the same on the .NET platform. The “standard” languages on the platform have little to no support for functional programming, but there are several “non-standard” languages that do much better (like Nemerle). That’s the great thing about the Java and .NET platforms, all the choices.
Tough stuff for me.
I tried ocaml and it was not easy.
Alot of this (())((())))(()))
Can get pretty confusing.
“Because FP languages do not contain any assignment statements, values of variables, once assigned, never change.”
Then why is it called a variable? I always see this statement when trying to learn functional programming and it completely throws me off. What would help is an example of functional code containing one of these “variables” to demonstrate how it is assigned to “without containing any assignment statements” and used in some way.
I’ve tried learning functional programming a few times and the main hickup is that I’m not comfortable in command line like interactive programming environments. I like GUI IDEs like VS, JBuilder, SharpDevelop, Eclipse,… The other problem I have is doing normal useful things like getting data form a database, writing to a text file, building GUI for user interaction. All the LISP code I find is centered more around algorithms that I already understand or don’t care about and none seem to point to well written libraries for getting stuff done or how to integrate them.
For these reasons I was really hoping that Microsoft would push F# so that I could finally learn and play with a functional langauge and do something useful that can cooperate easily with my other code written in C#. Especially if I can get this all done within the same IDE without hacking things.
“””Then why is it called a variable? I always see this statement when trying to learn functional programming and it completely throws me off. What would help is an example of functional code containing one of these “variables” to demonstrate how it is assigned to “without containing any assignment statements” and used in some way.”””
You create a new block of lexical scope. It’s called a variable because it’s the equivalent to a mathematical or lambda-calculus variable.
It basically comes down to how many times do you explicitly change the same variable over and over when you aren’t in a loop or it’s not global (or in Common Lisp of dynamic extent)?
“””All the LISP code I find is centered more around algorithms that I already understand or don’t care about and none seem to point to well written libraries for getting stuff done or how to integrate them.”””
All the functional code I’ve ever written either does some sort of analysis and produces a latex/html/pdf report (or some other kind of static final product)
or is on the server side interacting with a webserver or has a socket to some interface that the user uses (being telnet, a GUI, or something else).
Could you please let us know what functional langauge and tools you were using. I personally would really like to try writing some functional code because there are many cases when using OOP that I feel limited as far as my ability to express my ideas elegantly.
When you say “new block of lexical scope” do you mean ()? because even though I can understand the sentence in parts the whole goes right over my head. Maybe I should do some studying of lambda calculus because it often turns out that the road blocks to learning complicated concepts is solved simply by understanding that in the context of a given field that words are redifined to have more or different significance than their common use.
Currently I’m really curious about functional langauges but then also I’ve been meaning to take a look at prototype langauges such as SUN’s SELF. If there any good resources out there somebody please guide me and others who are interested in the right direction. Thanks.
I’m currently using for a small-business payroll system I’m developing deploying Scheme (Bigloo compiler) which compiles to JVM bytecode. I’ve developed in the past using Common Lisp (cmucl as a host of unix system) and ocaml which compiles natively on a most of systems.
With regard to lexical scoping, yes it is another set of parens (or more specifically a let or let* in scheme/common lisp).
(let ((x 3))
… do something with x here…
(let ((another-variable-name (f x)))
… do something else here … )))
In C it’s the equivalent of declaring a new block. Since baring a loop it’s very rare you will need to set the same variable over and over. With a loop it usually gets converted to recursive call
sum = 1;
for( i = x; i > 0; i ++ ) {
sum *= x;
}
becomes:
(define (f x)
(if (zero? x)
1
(* x (f (- x 1)))))
However, the true power of functional programming is storing and application of lexical closures and functions.