functional programming rocks!
Its a quite different way of thinking though. Closer to the mathematical view of things,
which allows certain tasks to be done in a very elegant way, much better than in imperative languages.
Once you get the grip of it, its not more difficult than imperative programming imho. Its just... different
I haven't used Haskell that much, but some other functional languages.
I think i can give some tips:
Think of the task to do in terms of a function that calculates a result in the mathematical sense,
not in terms of a C function or PB procedure.
There are no loops in functional programming generally, to do things repeatedly, there
is recursion. Everything that can be done in a loop can also be transformed to a
recursive function call, so this is no limitation at all.
What seems confusing at first is that Haskell allows no side-effects, so you cannot
have a variable and then change its value.
(other functional languages have this too, but they have spechial constructs to allow it again in some way)
The difference is in how the language calculates a result. Languages like PB have a "state"
(the content of all variables/memory blocks + the current point of execution),
and a result is calculated by manipulating that state (change variables).
Functional lanugages only evaluate expressions. All the code is seen as one big
expression, and when the evaluation is done you have your result. No changing of
variables is needed here.
An example: Lets calculate the sum of all numbers 0 to 10 in PB:
Code: Select all
x = 0
For i = 0 To 10
x + i
Next i
Debug x
How do we do the same without loops and without changing a variable ?
Lets look how we could define a mathematical function to calculate this. Something like this:
Code: Select all
/ 0 if x = 0
f(x) = |
\ x + f(x - 1) otherwise
Lets implement this in PB:
Code: Select all
Procedure f(x)
If x = 0
ProcedureReturn 0
Else
ProcedureReturn x + f(x - 1)
EndIf
EndProcedure
Debug f(10)
No more loops. And we also never change the value of "x" anymore. All we do
is call f() again, which will have a new x with a different value.
This is how functional programming works.
Here is the same in Haskell:
Code: Select all
f x =
if x == 0
then 0
else x + f (x - 1)
"f 10" gives the same result as the PB program.
With the pattern matching feature of Haskell, it is even possible to express this
much closer to the mathematical definition:
Looks just like the definition above:
"if g is called with 0, the result is 0, else it is x + g(x - 1)"
simply elegant
Of course there is much more to functional programming, for example the fact that functions
can create functions, or the Haskell lazy evaluation which allows to create
infinite data structures (lists of infinite size for example).
All cool stuff that languages like PB or C simply cannot do.
@pdwyer:
This is not true. Functional languages can be very efficient, espechially those that
compile to native code. Of course for some algorithms their performance
is bad, but you can easily find such examples for any language.
Imho they are espechially good for implementing algorithms, because some things
can be expressed much more clearly than in other languages, so the implementation is easier to follow.
Also the closeness to the mathematical definition is a good point when it comes
to prooving that an algorithm actually works correctly.
(good luck prooving that for a C code that throws around pointers like hell...

)
Oops, too much text and out of time... have to go
