No subject


Tue Apr 3 04:57:33 UTC 2007


a number of advantages.  When it comes to modelling relationships
between different entities, F/L programming paradigms allow a much
more powerful, natural and error free approach; at least, this
is my impression from various things I've read.  Unfortunately
the F/L approach has its downsides.  It has difficulties with I/O,
and is often quite slow in comparison to imperative languages
(like C++).  The advantages of a language like C++ over F/L languages
particularly come to the fore when one wishes to perform "evolving
states" computations, ie you have a collection of interacting objects,
each with a state varying with run-time.  However in other aspects the
F/L approach has many things going for it.

One approach to programming is to attempt to "get the best from both
worlds".  There have been attempts to implement functional programming
ideas in C++.  The three libraries I have heard about are: LL (Lambda
Library), FC++ (Functional C++) and FACT (Functional Additions to C++
through Templates and classes).  I have not used any of these, but
would be interested in comments from people who have.

The STL library seems to implement some functional programming ideas.
That is, I suspect, one reason why it is so useful.  It introduces
the idea of applying a functor object to an entire container; very
Functional paradigm like.

It seems to me that another way of using a functional approach in
C++ is to use constants as much as possible.  In pure functional languages
the identifiers (letters or names) do not vary --- they are symbols
attached to different logical structures; this attachment does not
vary in time as occurs with imperative languages.  This behaviour
is very similar to that of const objects in C++.  Books on C++
often recommend using const objects as much as possible in order to
reduce errors and maximize the amount of reasoning that may be
performed at compile-time.

It seems to me that with C++ programming, we should be looking for
ways for using a "functional style" of programming wherever this
is possible and appropriate.  Unfortunately C++ sometimes makes
this hard.  One example is with the STL vector.

It would be nice to be able to setup a vector of constant members.
For example

class myClassTy {
private:
  int xVt; int yVt;
public:
  x() const {return xVt;}
  y() const {return yVt;}
  putX(int xAg) {xVt=xAg;}
  putY(int yAg) {yVt=yAg;}
  myClass(int xAg, int yAg) {
    putX(xAg); putY(yAg);
  }
};

and then do

vector<const myClassTy> myVec=(myClassTy(1,1), myClassTy(2,4), myClass(3,6));

but, as I understand it, the value type of STL containers must be
assignable, and constant types are not.

"What is the point to all this musing?", I hear some of you asking.
I suppose I am trying to work out the best way to approach C++
programming.  I have heard that functional programming has certain
advantages over imperative and I wish to incorporate some of these
 --- where this is possible in C++ --- in my programming approach.
Unfortunately I am a little unsure about the best way to proceed
and am looking for some suggestions.

Possible strategies seem to be:

* Try to use const more.  How can this be done?  How can the limitations
of C++ be overcome?

* Use the STL more.

* Learn one of the functional libraries: LL, FC++ or FACT.  But which one?
And if so, how do I best combine these functional components with normal
C++ programming?  And how should I structure my "learning process"?  Ie,
do I throw out my current C++ programming ideas and start from scratch,
or is there a good way of making a "gradual transition"?

Thanks in advance,

Mark.



More information about the tuxCPProgramming mailing list