# Thread: My mind is changing.

1. ## My mind is changing.

A few months back, I pretty much loathed functionnal programming; now I like it very much (even though I'm not as good with it as I am with procedural, but that will come). It's very nice to be able to write complex things in a couple of simple lines Let's hope that many other people can come to like FP, because it is VERY nice.

2. ## Re:My mind is changing.

You used to write procedural programs? EEEEWWWWW!!!

I'd die without functions. They make everything so much easier, especially when you need to do stuff more than once

What can I say about procedural? It's not functional (if you'll excuse the pun )

3. ## Re:My mind is changing.

Hum, procedural has functions. What I mean by functional is a language where variables are by default immutable, where functions can be used as function parameters, where the default (or at least, encouraged) method of looping is the recursion. That's what I mean by function programming. Let me help you with an example. I will write a function that takes a list of integers and returns the sum in both style.

Procedural:
let sum3 l =
let acc = ref 0 in
for i = 0 to ((List.length l) - 1) do
acc := acc + (List.nth l i);
done;
!acc
;;
What happens here? We send the list (l) to the function (called sum3). Next it initializes a mutable variable into which the sum will be stored. We then loop over every element of the list each time taking the number we're on and adding it to acc. When that is over, we return the value inside acc.

Now, let's see what it looks like in functional style.
let rec sum1 l = match l with
[] -&gt; 0
| h::t -&gt; h + sum1 t
;;
I'm not exactly sure how to explain in words how it works. I understand it, but explaining it, seems to be a little bit beyond my explaining abilities (I'm no teacher ) Maybe ndogg could help me on this one? Anyway, in this example no value ever changes, I don't use any mutable data type, and I use recursion; it is not tail-recursion though, so it will stack overflow when given too big a list. The tail-recursive version looks like this:

let sum2 l =
let rec loop acc = function
[] -&gt; acc
| h::t -&gt; loop (acc+h) t
in loop 0 l
;;
The difference with the previous example is that instead of storing the sum on the stack, it stores it in a variable.

4. ## Re:My mind is changing.

I'll expand on what GnuVince has been talking about.

The second example is a list summation function. When it is called, it adds the first item in the list, then takes it out of the list, and calls itself with the list.

What's so fun about functional programming languages is that everything is a return statement. With this in mind, you cannot think in terms of steps. You need to think in terms of complete tasks. You need to complete each task to accomplish a goal.

5. ## Re:My mind is changing.

The second example is a list summation function. When it is called, it adds the first item in the list, then takes it out of the list, and calls itself with the list.
[/quote]

And as I understand it, the summing starts when the function gets to its ending case ([] -&gt; 0). In this case, when the list is empty, the value of the function is 0, and then the other values are added to it.

By the way Feztaa, if you still use Perl, you are using a procedural language.

6. ## Re:My mind is changing.

I like to venture into both worlds. I started out in a procedural language called Basic when I was 7. Back then, I don't think there was any easy functional language. And learning Basic made assembler a lot easier to learn. But I also like C/C++. Most of my programs nowadays are a mix of the latter and assembler. The two are so different but mesh so beautifully. If you want to be a good programmer, you should at least understand both.

7. ## Re:My mind is changing.

I like to venture into both worlds.
[/quote]

C/C++ and Assembly are all procedural.

8. ## Re:My mind is changing.

My mind is still wrapping around Object Oriented C++, which I find infintely cool.

9. ## Re:My mind is changing.

Just out of curiousity, what languages are functional? The only one I can think of is Cobol.

10. ## Re:My mind is changing.

Just out of curiousity, what languages are functional? The only one I can think of is Cobol.
[/quote]

Cobol is ABSOLUTELY not functional. FAR FAR FAR FAR FAR FAR from it.

Functionnal languages:
ML (O'Caml, SML), Haskell, Miranda, Erlang, Lisp/Scheme, Dylan.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•