Live data from Hacker News

Why functional programming?

news.ycombinator.com

11–20 of 60 posts

Re: Why functional programming?

#11
This past week I bought three books about Erlang. I'm going to be studying and going through all of those books in the next month or two.

Why? Because I have the same questions you do, only I'm a much worse programmer/developer.

Insert Nike slogan here.

What's the worst thing that can happen by learning something?

Re: Why functional programming?

#12
short and sweet: Functional programming as a paradigm is "about" the following things as I understand it:

1) functions as first class citizens (this is a great read that relates to this: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...)

2) immutable data: mutable state is the enemy of threading/concurrency/parallelism. Things can change underneath you in unexpected ways causing unforeseen consquences. When the data never changes, this sort of thing becomes much easier.

3) Referential transparency: This is related to #2. Whenever you pass A into function Z, you get B. There's no chance of side effects or other possible outcomes. A goes in, B comes out.

Re: Why functional programming?

#13

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 is the best explanation of functional programming I've ever read.

Congrats.

Re: Why functional programming?

#15

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…

I asked, "When is FP the right tool for the job?"

"If you can picture your program like a pipe..." may well be the answer I was looking for. Thanks!

Re: Why functional programming?

#16
post #11

This past week I bought three books about Erlang. I'm going to be studying and going through all of those books in the next month or two. Why? Because I have the same questions you do, only I'm a much worse programmer/developer. Insert Nike slogan here. What's the worst thing that can happen by learning something?

> What's the worst thing that can happen by learning something?

If nothing else, opportunity cost. I can't use the same time to learn something else.

Re: Why functional programming?

#17
Why should you care about functional programming? Here are a few reasons:

1. It's different, especially from what it seems you're already doing. Therefore it is likely that if you do FP in earnest, you will learn new problem-solving techniques, many of which will prove useful elsewhere.

2. In FP, the abstraction and composition model – functions – scales down farther than do objects, modules, or compilation units. That is, functions remain practical in smaller places where objects or modules would be too expensive to insert, syntactically or semantically. In FP languages, then, you can use abstraction and composition even within the "leaves" of your logic, and that's where the bulk of your code actually lives.

3. Functions have a mathematical basis that you can draw on for intuition. In some FP languages, the mathematical parallels are so reliable that you can even calculate the code you need. Calculation can work in places where main force of intellect and the search for "aha! moments" fail.

There you go, three reasons: it's different, it not only scales up but down, and it comes with useful theory.

Re: Why functional programming?

#19
I think a lot of people gloss over a bit too quickly on the actual implications at a hardware level on what it means to have immutable state. If you have 10mb of numbers that you need to add 1 to, immutable state means you now have 20mb of numbers guaranteed. Allowing for mutable state means you can stick to the 10mb and mutate it.

So in general, forced immutable state means that any class of problems which require both high performance and a lot of mutation are going to be very poor candidates for functional programming. This is actually a large share of embedded programming and why the OP probably hasn't found that much use for it - embedded programming often comes down to accepting large amounts of data from sensors and manipulating it.

The problems where functional programming shine are where you have a chunk of data and need to create an answer from it. Given 10mb of data, should I do X or Y? Functional programming is a very good tool here because of aspects such as composability allowing for extra steps to be chained into a calculation, and immutable data ensuring that multiple steps can be run on the same data at the same time.

Hopefully that helps a bit in deciding when functional would be useful or less useful.

Re: Why functional programming?

#20
Functional programming is about composing functions with functions. Just like OOP is about composing objects with objects.

FP and OOP is not in conflict with each other and you can certainly use both. FP is more in conflict with procedural programming. With procedural programming you are teaching the computer how to do things step by step (for i while i < len i++). With FP, you declare what you want to be done, and the computer figure it out on its own on how to get it done. An example would be for each loop on a collection. You are not telling the computer to increment index. You just want to iterate each element. The computer is free to spawn 20 threads to iterate the collection, so long that it accomplish the goal of your expression.

Post reply on HN