Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › functional programming
New Posts  All Forums:Forum Nav:

functional programming

post #1 of 13
Thread Starter 
This seems to be quite a popular buzzword at the moment. And while there are purpose build functional languages, I do keep reading about functional design being applied to imperative programming languages and from the what I can gather, it sounds like I tend follow some of the principles of functional programming already.

So just wondering who here uses functional programming languages and/or applies some of the functional paradigms to their program design in imperative languages (eg Python, C++, Java, etc)
post #2 of 13

I use Lisp quite a lot along with the occasional bits of J, and matlab (which is actually a vector-based functional language pretending to be like C). I use Javascript a lot and it's basically scheme with bad syntax and without some of the good features (it's undoubtedly the most used functional language). Python and Ruby have built-in functional programming (I use both of them), but Guido seems to dislike some aspects of functional programming which makes them more difficult compared to Ruby.

 

I'm still planning to learn haskell, but I haven't yet found the time.

post #3 of 13
Thread Starter 
Quote:
Originally Posted by hajile View Post

I use Lisp quite a lot along with the occasional bits of J, and matlab (which is actually a vector-based functional language pretending to be like C). I use Javascript a lot and it's basically scheme with bad syntax and without some of the good features (it's undoubtedly the most used functional language). Python and Ruby have built-in functional programming (I use both of them), but Guido seems to dislike some aspects of functional programming which makes them more difficult compared to Ruby.

I'm still planning to learn haskell, but I haven't yet found the time.

Would you mind giving us some insights into functional programming and possibly a quick overview of the built in bit in Python and Ruby that enable functional programming (bar in mind I've not really coded in either - so I'm more interested in how an imperative language can be used to write functional code).

I'm still wrapping my head around a lot of this because most of the stuff I've read on functional programming isn't all that concise and I've never really coded in a purely functional language (I don't really class Javascript as functional even in spite of it's Scheme influences).
post #4 of 13
Quote:
Originally Posted by Plan9 View Post


Would you mind giving us some insights into functional programming and possibly a quick overview of the built in bit in Python and Ruby that enable functional programming (bar in mind I've not really coded in either - so I'm more interested in how an imperative language can be used to write functional code).

I'm still wrapping my head around a lot of this because most of the stuff I've read on functional programming isn't all that concise and I've never really coded in a purely functional language (I don't really class Javascript as functional even in spite of it's Scheme influences).

Ruby (and python to a lesser extent) is all about applying functional techniques to block scoping. Here's two links on functional programming in ruby. Matz has said he was heavily inspired by lisp (in addition to other languages).

 

Javascript is a functional language. Functions are first class -- in fact, the first problem most programmers have is that javascript only has lexical (ie. functional) scoping for variables. The second major stumbling block is that closures are a necessity for good JS technique (for example, the canonical method of creating getter and setter methods is through a closure). All the nice functional aspects expected of a functional language are either built in or easily added (as Douglas Crockford has shown many time). It is telling that the most popular Javascript library (jQuery) is largely based around the rather extreme functional concept of monads (even lisp doesn't bother with monads by default).  Here's a lecture by Crockford on just how functional Javascript is.

post #5 of 13
Thread Starter 
Quote:
Originally Posted by hajile View Post

Javascript is a functional language. Functions are first class -- in fact, the first problem most programmers have is that javascript only has lexical (ie. functional) scoping for variables. The second major stumbling block is that closures are a necessity for good JS technique (for example, the canonical method of creating getter and setter methods is through a closure). All the nice functional aspects expected of a functional language are either built in or easily added (as Douglas Crockford has shown many time). It is telling that the most popular Javascript library (jQuery) is largely based around the rather extreme functional concept of monads (even lisp doesn't bother with monads by default).  Here's a lecture by Crockford on just how functional Javascript is.
A great many languages have first class functions but are clearly not functional languages (Perl, Go, PHP, Lua, to name but 4 off the top of my head). It's really not a unique feature to functional languages any more. And I wasn't arguing that there isn't a high degree of functional capabilities in Javascript (I wasn't trying to claim it's a pure imperative language either).

I think it's fair to say that Javascript is a multi-paradigm language (in that it can be used to write functional code, and various styles of imperative code), but I still wouldn't say it's a pure functional language because it's also has a heavy focus on imperative styles as well. Much like how you didn't class Python as a purely functional language. In fact Python and Javascript are very alike in that regard (both have first class functions and both can be used to write code in multiple different paradigms)
post #6 of 13
Quote:
Originally Posted by Plan9 View Post


A great many languages have first class functions but are clearly not functional languages (Perl, Go, PHP, Lua, to name but 4 off the top of my head). It's really not a unique feature to functional languages any more. And I wasn't arguing that there isn't a high degree of functional capabilities in Javascript (I wasn't trying to claim it's a pure imperative language either).

I think it's fair to say that Javascript is a multi-paradigm language (in that it can be used to write functional code, and various styles of imperative code), but I still wouldn't say it's a pure functional language because it's also has a heavy focus on imperative styles as well. Much like how you didn't class Python as a purely functional language. In fact Python and Javascript are very alike in that regard (both have first class functions and both can be used to write code in multiple different paradigms)

Lisp is multi-paradigm, yet it is commonly classed as functional. An imperative programmer can grind out imperative code in any language. Having used JS a LOT in web app development, I can assure you that doing anything in JS beyond basic onClick style oneoffs requires the use of the functional aspects of JS. The style of the code doesn't affect what a language is at it's core. For example, McCarthy originally wanted lisp to look like fortran by using M-expressions even though time would prove that lisp programmers prefer S-expressions (the two are equivalent with M-expressions reducing to S-expression trees before evaluation just like C code is reduced to a tree for evaluation). In my opinion, the biggest difference between imperative and functional languages is the scoping style. Imperative languages such as C, python, java, etc all use block scoping while primarily functional languages such as lisp, haskell, JS, etc all use lexical scoping.

post #7 of 13
Thread Starter 
Quote:
Originally Posted by hajile View Post

Lisp is multi-paradigm, yet it is commonly classed as functional. An imperative programmer can grind out imperative code in any language. Having used JS a LOT in web app development, I can assure you that doing anything in JS beyond basic onClick style oneoffs requires the use of the functional aspects of JS. The style of the code doesn't affect what a language is at it's core. For example, McCarthy originally wanted lisp to look like fortran by using M-expressions even though time would prove that lisp programmers prefer S-expressions (the two are equivalent with M-expressions reducing to S-expression trees before evaluation just like C code is reduced to a tree for evaluation). In my opinion, the biggest difference between imperative and functional languages is the scoping style. Imperative languages such as C, python, java, etc all use block scoping while primarily functional languages such as lisp, haskell, JS, etc all use lexical scoping.

Interesting stuff. Are you able to expand on what the difference is between block scoping and lexical scoping because after a bit of Googling (ok, just Wikipedia redface.gif ) the results that come up say that ALGOL based languages (eg Pascal) and C all use lexical scoping - yet they're the very definition of imperative languages. Now I'm not trying to dispute your comparison as I'm not familiar with those terms and it's pretty obvious that you understand these concepts (and particularly functional programming) a great deal better than I. But I am rather confused now. frown.gif
post #8 of 13
Hey, Plan9.

I would just like to point out that you're using the term "pure functional language" to actually mean "primarily functional language". There is, however, a difference between the two. Any language can be considered functional, if it supports the functional paradigm, but a pure functional language is one that doesn't allow side-effects. As in, no changes are made to pre-existing data. You might want to read up on the wiki article of "purely functional", if you wish.

Also, correct me if I'm wrong, but I don't see why you're comparing functional to imperative languages. As far as I know, the two aren't mutually exclusive. It's just that most pure functional languages happen to be declarative, instead of imperative.

As to your original question, the only real functional language I have used is Haskell (not including Python, Java, C or anything else, because even though they support functional programming, they aren't primarily functional). It's fun to use and allows for some incredibly beautiful algorithmic solutions to some programming problems, but it isn't terribly popular in the mainstream.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #9 of 13
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

Hey, Plan9.

I would just like to point out that you're using the term "pure functional language" to actually mean "primarily functional language". There is, however, a difference between the two. Any language can be considered functional, if it supports the functional paradigm, but a pure functional language is one that doesn't allow side-effects. As in, no changes are made to pre-existing data. You might want to read up on the wiki article of "purely functional", if you wish.
I know what pure functional means and I'm well aware that languages can support the function paradygm as well as other language paradigms. This is why I made the point of saying "pure functional" in terms of some languages to make the distinction between multi-paradigm languages and ones that are not. (ie the point you've now just reiterated).
Quote:
Originally Posted by {Unregistered} View Post

Also, correct me if I'm wrong, but I don't see why you're comparing functional to imperative languages. As far as I know, the two aren't mutually exclusive. It's just that most pure functional languages happen to be declarative, instead of imperative.
I'm well aware they're not mutually exclusive. I've also made that point several times in this thread.

This thread was really the concepts I was trying to compare - and that was only because I'm still trying to wrap my head around the full complexities of functional programming (I keep reading that it's supposed to be very hard concept, yet I've found it remarkably easy to apply what I've understood to be functional paradigms to my projects (and have always written code that way). Which leads me to think that I've not completely understood the full complexities)

The arguments about which languages are of what paradigm is more a tangent that myself and hajile have taken - albeit one that I've also found quite interesting.
Quote:
Originally Posted by {Unregistered} View Post

As to your original question, the only real functional language I have used is Haskell (not including Python, Java, C or anything else, because even though they support functional programming, they aren't primarily functional). It's fun to use and allows for some incredibly beautiful algorithmic solutions to some programming problems, but it isn't terribly popular in the mainstream.
How would you describe the differences between writing functional and imperative code?
post #10 of 13
Quote:
Originally Posted by Plan9 View Post

I know what pure functional means and I'm well aware that languages can support the function paradygm as well as other language paradigms. This is why I made the point of saying "pure functional" in terms of some languages to make the distinction between multi-paradigm languages and ones that are not. (ie the point you've now just reiterated).
I'm well aware they're not mutually exclusive. I've also made that point several times in this thread.

This thread was really the concepts I was trying to compare - and that was only because I'm still trying to wrap my head around the full complexities of functional programming (I keep reading that it's supposed to be very hard concept, yet I've found it remarkably easy to apply what I've understood to be functional paradigms to my projects (and have always written code that way). Which leads me to think that I've not completely understood the full complexities)

The arguments about which languages are of what paradigm is more a tangent that myself and hajile have taken - albeit one that I've also found quite interesting.
How would you describe the differences between writing functional and imperative code?

Great. Sorry for the confusion.

Basically, in functional code, you write the code as an expression that you want the computer to return, without caring how exactly the computer deals with it. The easiest way to describe is using an analogy of functional recursion. Functional languages (which are declarative) don't allow a sequence of instructions or standard while/for loops, and therefore, must rely on recursion. So, for example, if you're writing a number that finds the factorial of a number n, you wouldn't use a for loop with a counter that goes from 0 to n, but the standard recursion algorithm. So, essentially, your whole mindset should change when you switch to functional programming from imperative programming. You don't want to tell the computer how to do something, but what to return, in terms of the data you have.
Further points might be that, pure functional languages don't modify data or variables once they're set, and don't cause any external side effects. Also, since the whole code is basically just a set of functions, the code is much more re-usable and adaptable, allowing for libraries of pre-defined functions to be included with a language specification, making it higher level, in a sense.

In general, functional code is much more concise and easier to read than imperative code, and therefore, allows for spending less time coding, but more time thinking about a solution.

So, even though, the idea of functional programming might not be very hard to grasp, actually changing your mind to think in a completely different way is much harder.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › functional programming