Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Coder's Corner Lounge (http://www.programmingforums.org/forum11.html)
-   -   Functional Programming Languages (http://www.programmingforums.org/showthread.php?t=12045)

ZenOswyn Nov 28th, 2006 11:29 PM

Functional Programming Languages
 
All of my programming experience has been in languages such as C++, Perl, PHP, and Python. Maybe it's because of Pugs (or maybe I'm just expanding out and trying to learn more), but I've been hearing a lot about functional programming languages lately, like Haskell, Scheme, OCaml and the like.

I've seen some examples of these, and as I've never been exposed to them before, find them pretty difficult to understand. Where as with Python and C++, I could look at some example code not knowing the language and piece together what each line meant.

So, my question for you all of you is: Do you think there's a benefit to learning one (or more) of these languages? Of course I don't want to limit "these languages" to the ones I listed above as examples. There's also the question of usefulness. In what I've read on the subject, which is arguably not very much, I don't see functional languages being spoken of outside classrooms.

My lack of experience with them leads me to believe that if I had to solve a problem using Perl and Scheme I'd have a completed Perl script and a fist through my monitor while working with the latter.

Arevos Nov 29th, 2006 4:16 AM

Quote:

Originally Posted by ZenOswyn (Post 119990)
So, my question for you all of you is: Do you think there's a benefit to learning one (or more) of these languages?

Definitely. Not only does it give you another perspective on programming languages, it also forces you to think in a different mindset. Procedural programming allows one to think chronologically; first the program does x, then y, then z. Functional programming forces you to think about the overall structure; the program should to x, which is made up of y and z, which in turn is made up by a, b, c, d, etc.

I'll give you a few examples. The solution I devised for a long term programming project I'm working on for my company has its roots in Haskell's function composition. In addition, the functor standard I created for it owes much homage to Haskell's functions and monad system.

Another example is a javascript Ajax scroller system I was working on. I originally used the LiveGrid object from the Rico toolkit, but I found it to be poorly designed. I created my own, and in order to get the system straight in my head I took a bottom up functional approach, constructing a base until it was sufficiently complete that I could design my buffering algorithm without getting bogged down in the details of the problem.

Of course I don't want to limit "these languages" to the ones I listed above as examples. There's also the question of usefulness. In what I've read on the subject, which is arguably not very much, I don't see functional languages being spoken of outside classrooms.

Quote:

Originally Posted by ZenOswyn (Post 119990)
My lack of experience with them leads me to believe that if I had to solve a problem using Perl and Scheme I'd have a completed Perl script and a fist through my monitor while working with the latter.

Start simply to begin with, and work up from there. Functional languages are such a paradigm shift that you need to relearn basic concepts in order to work efficiently in them. But succeed in that, and you'll vastly expand your perspective.

mackenga Nov 30th, 2006 7:47 PM

I'll second that. I hadn't touched any sort of functional programming until they taught us Haskell at University and it was a real brain twister at first, but it actually feels very natural after a while. It's a very pure kind of programming. With imperative languages you can grunt along through the steps without every really being a particularly good programmer but functional programming really makes you distill the solution before you start. Haskell didn't just make me a better programmer, it improved my maths dramatically and gave me a much better feel for the difference between an elegant program and one that just shuffles along like a zombie.

I think the main reason you don't see a lot of functional languages in use in the 'real world' of IT is that there just aren't very many good programmers out there doing it for a living to be honest. Companies would rather their software developers produced code that did the job, no matter how much it creaked, without being able to command too high a salary. Languages like Haskell, LISP and friends do see the light of day outside of classrooms, but apart from in the world of free software (where Scheme and LISP make several notable appearances) and academia there's more interest in brainfarts like VB than in the joy of good code.

If you love programming, learn Scheme. A bit of a strange recommendation from me because I don't actually know it, but it's a descendant of LISP so I'm sure it's gorgeous and since it seems to give the GNU crowd wood it's probably not a bad horse to back in terms of practical usefulness either.

Klipt Dec 2nd, 2006 9:12 AM

AFAIK python is a functional language too - only people don't notice it because it can also do things imperatively. Check out Python for Lisp Programmers for a comparison.

Some functional features python doesn't have:
-Continuations: present in scheme and ruby, absent in common lisp
-Tail recursion optimisation: present in scheme, absent in the common lisp standard (but implemented by some environments, e.g. CLisp)

There is/was something called "stackless python" being developed to implement them...

Arevos Dec 2nd, 2006 10:21 AM

Quote:

Originally Posted by Klipt (Post 120363)
-Continuations: present in scheme and ruby, absent in common lisp

The latest version of Python, 2.5, has support for continuations via the bidirectional yield statement.

I also wouldn't class Python as a functional language. It has features in common with functional languages, but Python is very much a procedural language.

Python also lacks macros and parameter matching.

ZenOswyn Dec 2nd, 2006 2:45 PM

Thanks for the great replies. I found a free book on Ocaml, so I started to crawl through that.

Klipt Dec 3rd, 2006 3:39 PM

Quote:

Originally Posted by Arevos (Post 120364)
Python also lacks macros and parameter matching.

I've been trying to learn some Lisp and the macros look pretty awesome - but do all functional languages have that feature?

What's parameter matching? (Google gives something about C++ which seems related to function overloading?)

(And I think I misrepresented stackless - the site talks mostly about threads, and while continuations are mentioned as part of how stackless was written, I don't think it allows you to use them explicitly.)

Arevos Dec 3rd, 2006 4:05 PM

Quote:

Originally Posted by Klipt (Post 120474)
I've been trying to learn some Lisp and the macros look pretty awesome - but do all functional languages have that feature?

No, and Lisp/Scheme is the only language I know of that makes such extensive use of the feature, mainly because Lisp is essentially just a dressed up AST.

Most languages are turn code into an AST (that's an Abstract Syntax Tree) before attempting to run or compile it. This allows the computer easier access to the contents of the code, without having to reparse the text over and over again.

For instance, if a compiler came across the code "x = 2 * y + 1", it might convert it to the AST shown below:
:

    =
  /  \
  x    +
      / \
      *  1
    / \
    2  y

If one tried to represented this AST in text form, one might choose to enclose each branch of the tree in brackets. Something like:
:

(= x (+ (* 2 y) 1)
Look familiar?

Because Lisp is essentially just an AST written out in text, one can manipulate the AST (and hence the syntax of the language) very easily. Macros can be written without much special syntax; other languages which support Macros have to resort to more complicated libraries and functions.

Quote:

Originally Posted by Klipt (Post 120474)
What's parameter matching? (Google gives something about C++ which seems related to function overloading?)

It's a more sophisticated version of function overloading, best demonstrated with an example (in this case, in Haskell):
:

sum []  = 0
sum x:xs = x + (sum xs)

Note that sum is defined twice over, with different parameters. The first version will be called if it receives an empty list, []. The second version will be called if it receives a list with at least one character, and then splits it into the head of the list (x), and the tail (xs).

The above function is equivalent to the following in Python:
:

  1. def sum(xs):
  2.     if xs == []:
  3.         return 0
  4.     else if len(xs) >= 1:
  5.         x = xs[0]
  6.         xs = xs[1:]
  7.         return x + sum(xs)



All times are GMT -5. The time now is 1:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC