Functional Vs Procedural Vs Object Oriented Programming

From TexTheProgram: TeX is the last hurrah of procedural/functional structured (read: non-OO) programming.

Ok, I've finally taken the time to learn smalltalk. I really like it. I've been procedural programming for years, followed by years of that OO/procedural mishmash that people use when they teach themselves C++ without the help of any C++ books. I now am trying to conquer FunctionalProgramming.

What I fail to see is why people these days are so down on functional programming. As I get better with it, I want to use it more and more. I like smalltalk, but that isn't what I want to grab when I go home at night.

Perhaps a better ideal would be to combine the two. Complex mathematical systems could be expressed as functional programming, which could then be wrapped as a smalltalk object to send messages to.

-- Joshua Boyd


Joshua - have you considered CommonLisp? I mix procedural, OO and functional programming daily. Right tool for the right job, and all that... -- AlainPicard


I find that Python has the right blend of readability and availability in the average workplace. It (the latest version: 2.2) combines some good ideas from functional programming with a very appealing OO syntax. Having done perl, some lisp, lots of C++ and java, I find myself almost always reaching for a PythonLanguage solution. -- RoyMathew


It strikes me as peculiar that no-one has mentioned ObjectiveCaml yet. I find it a very pleasant mix of functional programming and object oriented programming. Here's some chit-chat with the ReadEvalPrintLoop:
    # class dog = object
        method speak () = print_string "Woof!"; print_newline () 
      end;;
    class dog : object method speak : unit -> unit end

# class cow = object method speak () = print_string "Moo!"; print_newline () end;; class cow : object method speak : unit -> unit end
I declare a class dog with a single method speak that says Woof!, and then a cow.

    # let rec repeat x n = match n with 0 -> []
                                      | n -> x :: repeat x (n - 1);;
    val repeat : 'a -> int -> 'a list = <fun>
I declare a function repeat that takes a value and a number and returns a list of n xes. Note that even though I have not specified any types, the compiler knows that it should expect one value of any type and one int, and return a list of the type passed first. This is called TypeInference.

    # repeat "foo" 3;;
    - : string list = ["foo"; "foo"; "foo"]

# repeat "foo" 3 @ repeat "bar" 3;; - : string list = ["foo"; "foo"; "foo"; "bar"; "bar"; "bar"]

# repeat "foo" 3 @ repeat 3.14159 5;; This expression has type float list but is here used with type string list
Note that a list cannot contain values of two different types.

    # repeat (new dog) 5;;
    - : dog list = [<obj>; <obj>; <obj>; <obj>; <obj>]
    # repeat (new dog) 5 @ repeat (new cow) 3;;
    - : dog list = [<obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>]
    # let animals = repeat (new dog) 10 @ repeat (new cow) 5;;
    val animals : dog list =
      [<obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>; <obj>;
       <obj>; <obj>; <obj>; <obj>; <obj>]
A list cannot contain values of different types, but I was able to create a list of cows and dogs! This is because the type of an instance of dog does not refer to the class, but its contents. Since cows and dogs have the same methods, treating them as of the same type cannot cause any runtime errors.

    # List.iter (fun animal -> animal#speak ()) animals;;
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Woof!
    Moo!
    Moo!
    Moo!
    Moo!
    Moo!
    - : unit = ()

-- MikaelBrockman

EditText of this page (last edited October 8, 2006) or FindPage with title or text search