Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39

Thread: My mind is changing.

  1. #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. #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. #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
    [] -> 0
    | h::t -> 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
    [] -> acc
    | h::t -> 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. #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. #5

    Re:My mind is changing.

    [quote author=sans link=board=9;threadid=3762;start=0#37975 date=1024012999]
    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 ([] -> 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. #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. #7

    Re:My mind is changing.

    [quote author=Evil Honkey Kenshi link=board=9;threadid=3762;start=0#38037 date=1024065237]
    I like to venture into both worlds.
    [/quote]

    C/C++ and Assembly are all procedural.

  8. #8
    Guest

    Re:My mind is changing.

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

  9. #9
    Senior Member
    Join Date
    May 2002
    Posts
    394

    Re:My mind is changing.

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

  10. #10

    Re:My mind is changing.

    [quote author=noblestknight link=board=9;threadid=3762;start=0#38088 date=1024078011]
    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.

Similar Threads

  1. Maybe this.......never mind
    By Fatal Error in forum General Chat
    Replies: 2
    Last Post: 03-09-2005, 01:27 PM
  2. Changing from XDM
    By mmiller9 in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 03-10-2003, 12:39 PM
  3. Mind if a Redneck using BSD plays with ya here ?
    By Redneck in forum Linux - General Topics
    Replies: 3
    Last Post: 09-21-2002, 01:36 AM
  4. This has been messing with my mind!
    By Digital_Froggy in forum Linux - Hardware, Networking & Security
    Replies: 3
    Last Post: 01-07-2002, 09:23 AM
  5. Just can't make up my mind, until now! LOL
    By paradox in forum Linux - Software, Applications & Programming
    Replies: 22
    Last Post: 12-20-2001, 09:18 AM

Bookmarks

Posting Permissions

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