in reply to RFC: A Perlesque Introduction to Haskell, Part One (DRAFT)

I've always understood the difference between functional programming and imperative programming (maybe some people don't even know that imperative is the term for "not functional") is basically this:

Anyway, describing functional languages by noting some of their "common features" is really not the way to go. It's a philosophical difference, all the way.

(Granted, as you noted, there is a limited concept of "state" in LISP, and, in general side-effect can't be completely avoided or you'd have no means by which to perform I/O... but you're getting into details, there.)

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: RFC: A Perlesque Introduction to Haskell, Part One (draft)
by danderson (Beadle) on Jun 23, 2004 at 22:05 UTC
    Interesting comparison. True, in functional languages solutions tend to be in the form of an answer given a valid input - but this is the same as non-functional languages. Perl's
    sub factorial { my ($i) = @_; return 1 if ( $i <= 0 ); return $i * factorial( $i-1 ); }
    is barely different from the generic functional code of
    factorial 0 : 1; factorial i : i * factorial( i - 1 );
    Both are solutions to the question 'what is factorial(i).' So, really, I'd say that coding in general is about forming well-defined questions, writing those down on paper, and then composing answers in code.

    Incidentally, I believe most functional languages have a full-fledged concept of state. Even Prolog, that bastion of 'tell us the rules, and we'll get you an answer' can be twiddled to spit out state at every recursion (otherwise debugging would be a royal pain). And it's worth mentioning that it's not uncommon to write a functional program based on a problem that's formed in state-machine terms, just because it's so easy to handle such problems.

    But programs as 'composing answers' - I like it. Nice thought.
      Well, to address your points:

      Yes, you can write functional code in (most) imperative languages... at least those that support recursive function calls. Although really the way to make a functional-like factorial call in perl would be like:

      sub factorial { $_[0] == 1 ? 1 : $_[0] * factorial($_[0] - 1) }
      That is: each function is only a single expression. Haskel goes a little further, though, because it supports function-dispatch by pattern matching... but this is the general case of how one writes functional code in perl. No assignments, no loops, a single expression. While it's true that any decent functional language interpretter supports automatic optimization of tail-recursion, that is a property of the interpretter, not of the language itself (except to the point that specifically optimized idioms usually become a part of any language, if only in the developers training of best practices).

      As far as the functional language interpretter having state: well of course it does. It's ultimately implemented in machine code, and machine code on any computer is imperative. It has state (memory, registers, etc). It is a sequence of commands. So on. The point is that this state is not a mechanism employed by the programmer in his/her functional programs, it is merely an artifact of how the functional language interpretter is implemented on an inherently imperative computation machine.

      Update: forgot the "- 1" in the code. Oops. I was just trying to make a point, anyway... it was obvious what I meant.

      ------------ :Wq Not an editor command: Wq

        Haskel goes a little further, though, because it supports function-dispatch by pattern matching...

        My mind has been full of Perl6 lately ... so bear with me ...

        multi sub factorial (Any where {$^n==0} $n) { 1 } multi sub factorial ($n) { $n * factorial $n-1 }

        ... which of course will recurse infinitely when given factorial 0.5 or factorial -1 ...

        ObTopic: How do the Haskell examples handle those cases?

        print "Just another Perl ${\(trickster and hacker)},"
        The Sidhekin proves Sidhe did it!