Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by etcshadow (Priest)
on Jun 24, 2004 at 02:17 UTC ( #369224=note: print w/replies, xml ) Need Help??


in reply to Re^2: RFC: A Perlesque Introduction to Haskell, Part One (draft)
in thread RFC: A Perlesque Introduction to Haskell, Part One (DRAFT)

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

Replies are listed 'Best First'.
Re^4: RFC: A Perlesque Introduction to Haskell, Part One (draft)
by Sidhekin (Priest) on Jun 24, 2004 at 02:29 UTC

    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!

      How do the Haskell examples handle those cases?

      It doesn't. You can use "boolean guards" though to handle different cases:

      fac 0 = 1; fac n | n > 0 = n * fac (n-1) | otherwise = ...
      otherwise is just a standard alias for True to make it read well.

      ihb

      Touche. I was referring to perl 5, not perl 6. But that's cool to know.
      ------------ :Wq Not an editor command: Wq

Log In?
Username:
Password:

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

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



    Results (113 votes). Check out past polls.

    Notices?