News for hackers. Stuff that matters.
Re:Functional Programming For The Rest of Us by rauljara on Tuesday March 20, 2012 @11:10AM (#3729621)
Kudos to the piece for being well written, readable, and clear. But I'm worried about a "for the rest of us" that starts off with first Plato and then lambda calculus.

The problem with "the rest of us" is that these concepts aren't inherently neat. Disclaimer: I'm not one of "rest of us". I'm learning Haskell, just because I enjoy having to think in a new way.

A lot of FP explanations start off with immutability. After plato and lambda calc, that's where this one goes, too. I think because it's a relatively simple concept, and kind of mind blowing that you could do something useful without incrementing i in a for loop. And then the follow up example is normally how you can replicate a for loop in functional programming. Which is again neat.

But if you aren't into neat things for their own sakes, you've got to ask why? Why replicate a for loop when you could just use one? (answer: you don't really replicate for loops in FP) They aren't asking why, in the sense that they're curious. They're asking why in the sense of "Why would anyone do that? That's stupid."

If you aren't into having your mind blown for no apparent reason (some aren't) the only really compelling thing about the first several pages of the piece is the "Benefits of FP". That's the motivation to learn these crazy new concepts. It should really come first. I think it should come first in any discussion of FP for people who aren't familiar with it.

Because for most people to learn, they need a motivation. Your programs will be faster because you can do concurrency super easy is pretty good motivation. Sadly, "because it's neat," is generally not.

Re:Functional Programming For The Rest of Us by tomp on Tuesday March 20, 2012 @11:58AM (#3729909)
This article is another one of those that confuses functional programming with pure programming (which is, I believe, by definition also functional).

You can do a lot of functional programming in a language where values are mutable. I use it all the time in python: the map and filter functions, list comprehensions, zip, ... For me, functional programming means having first-class functions that can be passed to/from other functions. A standard library of common functional combinators help, but you can always write your own.

Funny, but some of the most often mentioned functional languages, such as OCaml and Scala, allow liberal use of mutable variables. They just make it a bit harder/more explicit, so the programmer has to give more consideration into it.

Re:Functional Programming For The Rest of Us by jshen on Tuesday March 20, 2012 @12:03PM (#3729940)
You must have read a different article than the one I read.

"Functional programming is a practical implementation of Alonzo Church's ideas. Not all lambda calculus ideas transform to practice because lambda calculus was not designed to work under physical limitations. Therefore, like object oriented programming, functional programming is a set of ideas, not a set of strict guidelines. There are many functional programming languages, and most of them do many things very differently. In this article I will explain the most widely used ideas from functional languages"

No where does he say that functional programming is pure programming.

Re:Functional Programming For The Rest of Us by sharmajai on Tuesday March 20, 2012 @12:48PM (#3730233)
You can do a lot of functional programming in a language where values are mutable.

Sure you can do whatever you want, but if the language does not support things like tail call optimization, non-imperative function definition, writing code in functional style turns out to be a lot slower (due to non-tail-optimized function call overhead) and less safe (due to potential side effects in function definition) than if you are writing it in a functional language with explicit support for these facilities.

Re:Functional Programming For The Rest of Us by JadeNB on Tuesday March 20, 2012 @01:14PM (#3730362)
That's true, but this functionality is perfectly compatible with mutability.

Various flavours of Lisp have been in the forefront (CMA: among 'popular' languages) in making this functionality available, but I don't know of any (CMA: 'popular') pure flavour of Lisp. (Maybe Clojure --but its preference for 'immutable by default' is quite different from not having mutable values at all!)

Re:Functional Programming For The Rest of Us by zby on Tuesday March 20, 2012 @04:21PM (#3731319)
OK - I checked wikipedia:

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Sounds exactly what I was thinking - map is not mathematical function - because it can interact with the global state (you can apply map twice to the same data and get different results - for example if in the function you are mapping you use 'time') - and thus using it is not 'functional programming'. If your first-class functions were mathematical functions - then we could agree - but map is not one.

Re:Functional Programming For The Rest of Us by polymatter on Tuesday March 20, 2012 @07:06PM (#3732159)
"map is not mathematical function - because it can interact with the global state (you can apply map twice to the same data and get different results"

I don't think I quite understand you. I think when you say "mathematical function" you mean "pure function". And when you say "global state" I assume you mean "impure operations".

But (from my understanding) map is purely functional and doesn't do anything impure like IO. Its purely a mathematical mapping between a collection of input values to a collection of output values, based on applying a function to each input value in turn. You seem to be suggesting "map f (map f x)" (that is applying map twice to the same data) is not the same value as "map f x" and therefore its not "mathematical".

Re:Functional Programming For The Rest of Us by zby on Wednesday March 21, 2012 @02:48AM (#3733398)
OK - I can agree with the interpretation that the 'impurity' is not inside map - but rather in the argument it takes. You can reconcile it with the mathematical definition of function by saying that some 'programming functions' take the implicit 'state' argument and that map is not one of them.

It still does not resolve the definition problem - is functional programming programming with pure mathematical functions - or is it using first-class functions (not in mathematical sense)? Which definition is more useful? Which is more widely used?

Re:Functional Programming For The Rest of Us by tomp on Tuesday March 20, 2012 @04:33PM (#3731384)
I'm reading this differently than you are;

> functional programming is a programming paradigm

NOT a programming language. What I think this means is that your usage of map is such that the computation is what matters, not mutating the state. I can do that just as well in Python, JavaScript, OCaml, Scala, Clojure and Haskell. In other languages too, yes, but not nearly as easily (due to lack of closures/complicated functional types/no first-class functions/...).

If you were to insist that functional programming is just math (no mutable state), then even Haskell and Mathematica are far from functional.

Re:Functional Programming For The Rest of Us by zby on Wednesday March 21, 2012 @02:51AM (#3733410)
I insist that what is important is how much of the purity you have - because it is a more widely used definition (as supported by my wikipedia quote) and also this is what implies the nice computational properties of the code, and what is not important is if you use first class functions (not in the mathematical sense).
Re:Functional Programming For The Rest of Us by sold on Tuesday March 20, 2012 @07:42PM (#3732273)
This is a single introductory sentence. You should not reason that far with it.

With that reasoning you could claim addition is impure, because time() + 1 changes over time. map is definitely a pure function. If you apply it to impure arguments, of course the result will be impure. The point in FP is to avoid using 'time', not 'map'.

Re:Functional Programming For The Rest of Us by zby on Wednesday March 21, 2012 @02:35AM (#3733372)
That's a good argument - but it does not really resolve the definition problem. It seems that some people think that 'functional programming' means 'programming with mathematical functions' - while others think that it means 'programming with first-class functions'. I can take this or that definition - but if wikipedia uses one definition - this sounds like a good argument that more people use the 'mathematical functions' definition. I also think that 'mathematical functions' is a more useful definition - because it implies 'referential transparency' and probably other interesting things that are usually quoted as the advantage of using functional programming.
Re:Functional Programming For The Rest of Us by sold on Wednesday March 21, 2012 @12:50PM (#3735215)
> wikipedia uses one definition

Let me repeat: you used an introductory sentence as a "definition" of FP and are overinterpreting it. If you read later you will see:

> Functional programming languages, especially purely functional ones...

So Wikipedia distinguishes functional programming and pure functional programming. In fact there is a separate article on pure functional programming, which says that destructive modifications are "excluded", not just "avoided".

Functional programming does not have clear boundaries. You can have advantages of functional programming even if you introduce _a bit_ of FP style in a very imperative program. Conversely, you can write in very imperative style in Haskell.

Re:Functional Programming For The Rest of Us by zby on Thursday March 22, 2012 @02:50PM (#3741331)
My point is that the definition of functional programming is this purity you mention not using first-class functions. Pure means that something has more of that thing you want and impure means it has less - so pure functional programming has more 'functional programming' then 'standard functional programming'. Sure completely pure functional programming is impractical - there are degrees to how purely functional are the languages we use in practice - but this is the point practical languages are more or less functional and how much are they functional depends on how much they depend on state manipulation not on using higher order functions or having first class functions.

Defining functional programming as programming with first-class functions is a consistent definition and could be used if not the fact that more people use the other definition (at least that is what the wikipedia definition and other online material makes me believe).

Re:Functional Programming For The Rest of Us by sold on Thursday March 22, 2012 @06:50PM (#3742470)
Is Prolog a functional language, as it avoids state manipulation but has no first-class functions? I think not. There are some characteristics of functional languages - including first class and higher order functions, relying on immutability, declarativity etc. Choosing just one of them to measure how much a language is "functional" is a mistake IMO.
Re:Functional Programming For The Rest of Us by ImprovedSilence on Tuesday March 20, 2012 @12:26PM (#3730084)
I think by "rest of us", he meant nerdy, curious programmers. And not academic PHD mathematicians.

I don't know your background, maybe you are deep in the academic world, but if not, it's likely you are the "rest of us" he is addressing.

Re:Functional Programming For The Rest of Us by iron_ball on Tuesday March 20, 2012 @03:31PM (#3731075)
With that in mind, I recently wrote a series of posts about integrating functional programming concepts into everyday Javascript programming (and AS3, which is nearly identical). The last two, in particular, might be interesting even for programmers who already understand the basics.

http://www.alanmacdougall.com/functional-series/

I'd really like to see these techniques emerge into the spotlight, instead of just being voodoo magic hidden deep in the guts of frameworks.

Re:Functional Programming For The Rest of Us by novalis on Tuesday March 20, 2012 @06:20PM (#3731989)
Very interisting posts in there from an interesting point of view.
Re:Functional Programming For The Rest of Us by evincarofautumn on Tuesday March 20, 2012 @12:12PM (#3730003)
I agree. It’s fun for someone like me who likes learning new stuff for its own sake, but I think the “Rest of Us” might feel a bit left out without more practical examples. But hey, that’s the same mistake I made when I last wrote about concatenative programming. Hindsight’s 20/20.