Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2006, 11:29 PM   #1
ZenOswyn
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 ZenOswyn is on a distinguished road
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.
ZenOswyn is offline   Reply With Quote
Old Nov 29th, 2006, 4:16 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by ZenOswyn View Post
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 View Post
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.
Arevos is offline   Reply With Quote
Old Nov 30th, 2006, 7:47 PM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4 mackenga is on a distinguished road
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.
__________________
"I'm not a genius. Why do I have to suffer?"
mackenga is offline   Reply With Quote
Old Dec 2nd, 2006, 9:12 AM   #4
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
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...
Klipt is offline   Reply With Quote
Old Dec 2nd, 2006, 10:21 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Klipt View Post
-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.
Arevos is offline   Reply With Quote
Old Dec 2nd, 2006, 2:45 PM   #6
ZenOswyn
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 ZenOswyn is on a distinguished road
Thanks for the great replies. I found a free book on Ocaml, so I started to crawl through that.
ZenOswyn is offline   Reply With Quote
Old Dec 3rd, 2006, 3:39 PM   #7
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Quote:
Originally Posted by Arevos View Post
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.)
Klipt is offline   Reply With Quote
Old Dec 3rd, 2006, 4:05 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Klipt View Post
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 View Post
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:
Python Syntax (Toggle Plain Text)
  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)
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What programming languages and programs do online casinos use? Splashedwater Other Programming Languages 9 Jun 6th, 2005 3:53 AM
What programming languages should I learn? theduck Other Programming Languages 6 May 28th, 2005 10:34 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:17 AM.

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