Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: RFC: A Perlesque Introduction to Haskell, Part One (draft)

by etcshadow (Priest)
on Jun 23, 2004 at 21:19 UTC ( #369183=note: print w/replies, xml ) Need Help??


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:
  • Imperative programming means just that: you are giving orders (or "instructions", if you want to put it nicely) to the computer.
  • Functional programming is about evaluation. It's about getting answers to questions. "What is this thing squared?" "What is the representation of this data as a binary search tree?". It's not about doing things. Rather, it's about composing answers to questions. Things like ordering of events and even the passage of time itself (time in programs often being measured in terms of operations, or in terms of tracing through program execution one step at a time) don't really exist in functional programming.
  • Probably the biggest direct consequence of this is that functional programming has no concept of "state", as imperative programming does. "State" being the current value of all the variables (or memory or disk or however you want to think about it) being used by your program. "Current" is an important part of that, because these values change over time (after all, you're giving orders like "set this variable's value to 5" all over the place), and as I said... there's not the same concept of time in functional programming.

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://369183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2019-11-22 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (113 votes). Check out past polls.

    Notices?