Live data from Hacker News

Why functional programming?

news.ycombinator.com

51–60 of 60 posts

Re: Why functional programming?

#51
post #44

Earlier quoted context omitted.

Actually I think the poster is right, and I don't think we should avoid all lines of argument that might be subject to personal bias. The poster was saying that FP has a certain cachet (no pun intended) that is probably due to the quality of the average programmer. It's a bit like if someone was asked to explain higher salaries in Silicon Valley. Are we allowed to refer to the quality of programmers? Of course there…

You'd have to bring in actual results: programmers in SV create great products that are widely used around the world. This could be used as evidence to imply they are better. FP is a bit tougher here - the vast majority of programs used by the general public are not FP. You could argue that FP is 'newer' and not had time to create these widely used programs yet - but FP is far from new. Basically there is no real wor…

This is kind of the dirty secret of any kind of programming, that it's very hard to make arguments that this or that technology leads to categorically better or different software. I mean, yes, there are specific domains, like trying to write webapps in C vs Ruby, but in general the programming languages well-suited for a task tend to be much, much more alike than different.

We love to hate on PHP but it basically runs a non-trivial proportion of the web, incl Facebook. Python and Ruby look different but they're not so different that we can say, on average, a programmer is X times more productive using language Y instead of Z.

That said I am kinda sorta sympathetic to the idea that people who veer off the beaten path of Java are self-selecting and may be open to more ideas, more adaptable, etc. But I'm leery of extending that to saying they're "better" in any measurable or definitive way.

The one thing I would say re: FP being "newer" is that while the ideas aren't newer, it's "newer" in the sense that it's not as battle-tested as Java or C. Although, then again, when you see Ruby (a friendlier, no parens Lisp-alike), JS, higher-order functions in C++, list comprehensions in Python, and so on, it stops looking quite so much like FP is really all that foreign. People are already using a lot of it. So perhaps I should be more specific and say that while the concepts aren't new, the implementations are new to industry or production systems.

Re: Why functional programming?

#52
Can anyone provide advice on which language to pick? I started learning C++ and Java in high school and college because those were forced by the curriculum. My first two jobs out of school forced me also to use C# which was close enough to Java. When I wanted to make the leap to script languages, my choice between Python and Ruby basically boiled down to the strength of the local Ruby / Python communities, and Ruby clearly won in that realm.

But with functional programming, I want to learn, but I have far less direction. Scala is perhaps the biggest, but still small. Haskell seems to be the most "hardcore", which appeals to me, whereas I keep anecdotally seeing people who like Ruby and Javascript (my two main languages) also tend to gravitate towards Clojure, and I can't explain why.

So far I've hated Scala's syntax and formatting (much like I hated Objective-C). Haskell is making my brain melt. Clojure seems quite simple and reminds me of my days writing Scheme in school. Any thoughts?

Re: Why functional programming?

#53
Because functional code is on average half the size of imperative code. The shorter the code, the better. If that argument doesn't convince you, then you need to first understand why writing short code is so important.

Re: Why functional programming?

#54
post #36

Earlier quoted context omitted.

Something like "map myints (+ 1)" can be performed in a mutable way, without compromising on functional purity. (So long you're not using the old values afterwards, of course.) Not all compilers perform such optimizations, but there's no fundamental conflict. I believe Haskell's GHC compiler has "fusion" which allows it to wrap chains of maps/filters/folds into a single tight loop. That way you keep functional style,…

You're getting a bit bogged down in details, I meant it more as a general case. There are numerous cases where it is simply more efficient to be operating directly on data - the most used example I can think of are fragment and vertex shaders. It's irrelevant to the issue anyway though: simply having the ability to do something does not make it the best way to do something. You could code in a composable and immutabl…

Even when doing direct mutation of a byte array, FP can come in handy. The key part is being able to pass around and return functions. While I don't have experience "manipulating blobs of sensor data", most code can benefit from passing functions around and using closures. Even in otherwise imperative code, using folds and maps may make things clearer. Although, it might not be practical due to performance reasons.

Re: Why functional programming?

#55

I don't have an answer for you, but for the last year I have been learning Clojure, and I have found it fascinating. I do not think this will satisfy you, but I would like to share some of the things that I find interesting about Clojure. Lately I've been studying the source code of Aleph, a web server written by Zach Tellman. Zach is a very smart guy, and reading his source code is an education (I've actually found…

Uh, hey, I'm Zach. Thanks for the shout out. I'm mildly uncomfortable with you using Aleph as an example of good, idiomatic Clojure, but I suppose I can live with it.

If you're looking through my code in the future and have questions, please feel free to get in touch.

Re: Why functional programming?

#56

I would be interested in your views about OOP; it shares some ideals with FP, namely, encapsulation. OOP puts a lot of emphasis on encapsulating state, and binding the associated behavior to it so that no other part of the program can access it and cause problems, and so that contracts can be expressed by clean, abstract interfaces. It also typically uses procedural workflow and algorithmic and adds to it classes as…

This was really excellent

Re: Why functional programming?

#57

I don't have an answer for you, but for the last year I have been learning Clojure, and I have found it fascinating. I do not think this will satisfy you, but I would like to share some of the things that I find interesting about Clojure. Lately I've been studying the source code of Aleph, a web server written by Zach Tellman. Zach is a very smart guy, and reading his source code is an education (I've actually found…

Much of Clojure itself is written in Clojure. I would recommend going right to the source for idiomatic examples. https://github.com/clojure/clojure

Re: Why functional programming?

#58

Can anyone provide advice on which language to pick? I started learning C++ and Java in high school and college because those were forced by the curriculum. My first two jobs out of school forced me also to use C# which was close enough to Java. When I wanted to make the leap to script languages, my choice between Python and Ruby basically boiled down to the strength of the local Ruby / Python communities, and Ruby c…

I'm a bit biased, but I'd recommend giving Scala a try. There is a lot of non-sense floating on the internet about the language, but I suggest making up your own mind.

Tooling and IDE support is better than Haskell and Clojure, so you can focus on the language instead of fighting with tools.

If you know Scala a bit, it's easy to move to something else. For instance, if you decide that you don't need types, you can move to Clojure; if you decide you don't need unchecked side-effects you can move to Haskell; if you decide that you don't need functional programming, you can move to a different OOP language (or keep using Scala).

Re: Why functional programming?

#59
post #57

I don't have an answer for you, but for the last year I have been learning Clojure, and I have found it fascinating. I do not think this will satisfy you, but I would like to share some of the things that I find interesting about Clojure. Lately I've been studying the source code of Aleph, a web server written by Zach Tellman. Zach is a very smart guy, and reading his source code is an education (I've actually found…

Much of Clojure itself is written in Clojure. I would recommend going right to the source for idiomatic examples. https://github.com/clojure/clojure

I used to agree with the common objection that Clojure has poor documentation coverage, but then I realized that, usually, reading the source code was an excellent way to get a handle on things, given the habit of functional purity that Clojure developers seem to have. This is even more true with tools like Cider and Fireplace, which give you instant access.

Re: Why functional programming?

#60

I can appreciate your position, because I mostly write programs that model the physical world. The state is what you care about, and the state changes over time, and you have to solve for the state iteratively-- which means that the transition from state [i] to state [i+1] includes a bunch of intermediate guesses at the state. Furthermore, those state updates take place element by element-- you rarely update an entir…

I appreciate your comment, but I think it's inaccurate to say that functional programming is suboptimal whenever you're dealing with stateful systems.

For me, the primary characteristic of functional programming is that it hems pretty closely to mathematics. And the fundamental language of mathematics is "pure." The calculus that underlies engineering is expressed in a pure language where time is simply a function parameter -- and this in itself doesn't mean that calculus is ill-suited for dynamic problems.

If you look at the Haskell world, for example, there is a ton of fascinating work being done with stateful systems, reactive systems, and so on. Functional programming doesn't exclude state and dynamicity. But it insists on modelling these things mathematically, that is, with a foundation of immutability, equational reasoning, etc.

See what I mean? I'm not saying that current implementations of functional languages are perfect in every scenario -- that's clearly false. But basically, or theoretically, functional programming is eminently capable of modelling state, and could even be argued to model it more coherently and intelligently than other paradigms.

Post reply on HN