On 2/11/03 7:15 PM, [EMAIL PROTECTED] wrote:
> Can you briefly clarify "Functional Model" (even with just a link to
> somewhere else) since it can be easily confused with other definitions
> of functional programming?
>
> -Mark
Good question, Mark. I'll do my best without digging too deep into the
COMP411 archive...
Technically, a functional programming model is one in which everything is
expressed in terms of pure functions in the mathematical sense.... The old
algebra and calculus kind of function: f(x) = y. That is, you have
functions, you apply them to parameters, you get results, and you compose
results in various ways.
The functional paradigm, where you compute results by composing functional
expressions, is usually contrasted with the so-called "imperative" paradigm,
where you compute results by sequencing statements and performing
side-effects (generally speaking, assignments to variables).
A "Pure Functional" language can be somewhat usefully defined as those
languages that do not have "imperative" features. From this you can observe
that a pure functional language doesn't have variables (though it will often
have constants). Another distinguishing characteristic is that the
functional model says that Functions Are Values. This feature is commonly
called "first-class functions", and lets you define functions that accept
functions as arguments, and return functions as values, and construct
functions on the fly. (Java's anonymous inner classes is attempt at
approximating the value of first-class functions, but it's terribly clumsy,
verbose, and limited when compared to the real thing.)
Many people are surprised by the fact that you can do any real work that
way, but as it turns out, you can perform _any_ computation under such a
model, you just express things differently. (That is, the functional model
is Turing-complete.) If this blows your mind, google for "Lambda Calculus"
and be prepared for some really mind-warping stuff.
In the real world, however, imperative languages reign, and there are
relatively few purely-functional languages, and not a one that's commonly
used outside of academia. They _are_ used quite a bit in academia, since
they allow for very complete and rigorous mathematical models and analyses,
useful for things like proving correctness of programs. The Lambda Calculus
is in fact very widely used means of defining the semantics of programming
languages, whether they by functional or imperative or "other".
There are, however, quite a few "mostly functional" languages that provide
imperative features, but where the bulk of the programming is typically
done in a functional way. Lisp, Scheme, and ML all fall into this
category... In those languges the use of an assignment is a pretty rare
sighting.
[ Most OO languages are pretty aggressively imperative. It's damn near
impossible to write a useful Java program without using an assignment. ]
By "functional programming", language-weenies like me usually mean
"programming within the functional paradigm", and sometimes mean
"programming with a functional language". It's sometimes possible, but
usually frustrating, to do functional programming in an imperative
langugage.
For my money, the "big win" in the functional model is actually very simple:
eradicate the distinction between statements and expressions. If you've
ever used Lisp or Scheme (or, more recently, Dylan) you understand what this
means and how powerful it is. Basically, every programming language
construct has "a value".
The IF statement is a particularly nice example. If you write in Java,
if (test) { doSomething(); } else { doSomethingElse(); }
there is no notion of the IF as a whole having "a value". You can't use the
whole thing on the right-hand-side of an assignment, for instance.
However, the parallel construct in Scheme,
(if test (doSomething) (doSomethingElse))
does in fact have a well-defined value: if the value of test is true, the
value of the if-expression is the value of it's then-clause; otherwise it
evaluates to it's else-clause. So in the example above, the value would be
the result of calling one of the functions doSomething or doSomethingElse.
Before somebody notes that C/C++/Java do in fact have this notion in the
form of the conditional expression
(test ? doSomething() : doSomethingElse())
the point is that it's extremely useful to design your language so that
*every* construct has a value... So there's no difference between statements
and expressions (ie, imperative constructs and functional constructs).
So what does this have to do with Jelly? I first observe that the whole
structure of XML leads itself perfectly towards a functional paradigm, which
is very big on nesting, syntactic consistency, and substitutability. But
more convincing (to me at least) is the fact that a functional model would
enable a great deal more flexibility in our Tag Libraries, at much lower
effort. I guess what I'm really shooting for is to extend Jelly's
script/tag model so that it is easy to define tag libraries that work in a
purely-functional manner, an imperative manner, or anything in between. As
some who's worked with quite a few languages in my time, I recognize the
value of using the right model for the job. I'd like to see Jelly enable
that model so that it can be used all over the place!
Rather than repeat my earlier proposals, I'd encourage the interested reader
to search this forum's archive for the thread "Towards Functional
programming in Jelly scripts" that started 12/12/02. I would of course be
happy to renew those discussions again.
.T.
--
War is NOT a necessity!
http://UnitedForPeace.org/ http://MoveOn.org/
http://NotInOurName.net/ http://CWG.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]