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 b…
Don't the compilers optimize this? I always believed functional programming to be popular among compiler experts because it's easier to write certain optimizations on a program because of it's many properties like state not changing etc. I don't know if this leads to realistically faster programs in real life.
Why functional programming?
41–50 of 60 posts
Re: Why functional programming?
#42One of the perks of FP is that it exposes pure functions, functions that have a fixed output for a given input. Since it's really bad at storing and mutating state(canonically can only be done via monads), it encourages you to separate your code into pure and mutable states. Let's take, for instance, a function that reads a file and xor's that with a random byte stream. A non functional approach would be something like this:
void xor_file_stream(file input, stream random_stream){
result = []
file_byte = read_byte(input)
while(file_byte != EOF){
stream_byte = read_byte(random_stream)
res.append(file_byte^stream_byte)
file_byte = read_byte(input)
}return result
}
A possible way to write the same function using FP, using currying[1] and map[2](a fundamental construct in FP), one can write the code as this: byte xor(byte b1, byte b2){
return byte(byte b2){
return b1^b2
}
}
//Here I assume the language has file and random_stream as iterables
void xor_file_stream(file input,stream random_stream){
return map(map(xor,random_stream),input)
}
While, in a naive implementation, this would be much slower than the procedural implementation, there are much stronger assumptions one can make in respect to the functions while optimizing the compiler. First, the function xor is a pure function with 2^9 possible input values, and can be substituted by a precomputed lookup table, speeding up the map function. Since the function is guaranteed not to hold state, one can also unroll the map loops, or paralelize it if needed.But remember that most "pure" FP languages are bad at keeping state, turning a huge game(for instance) into a challenge both to the programmer and to the compiler.
[1] - http://en.wikipedia.org/wiki/Currying
[2] - http://en.wikipedia.org/wiki/Map_%28higher-order_function%29
EDIT: Formmating, not used to posting to hackernews comments
Re: Why functional programming?
#43I 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 b…
Re: Why functional programming?
#44Earlier quoted context omitted.
> I think in reality the appeal of FP is that the people who use it are generally more experienced programmers who tend not to make too much mess in non-FP languages anyway. The benefits are more the peer group than anything else. Careful, you're arguing here that FP is better because the person using it is a better person. There is nothing wrong with that statement if it is actually an unbiased fact, but simply choo…
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…
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 world evidence to back up that FP programmers are higher skilled and only personal biases and assumptions.
Re: Why functional programming?
#45Well, some toughts are easier to represent in abstract algebra. Really. Functional programming is too generical a label to make it possible to answer your question. It may apply to anything from C-like functions to no side effect languages, but I guess most people around here use the term to mean Lisp. If that's what you meant, Lisp'll give you a huge amount of reflexivity that you can use to change the language so i…
For the OP, let me see if this connects: In C/C++ and others, we have the ternary operator, which lets you write conditional expressions. status = n > 9000 ? "yes" : "no"; This is pretty convenient, but annoying to read afterwards, especially if you start nesting. Functional languages let you have the same convenience without the annoyance. You can do things like this OCaml: status := if n > 9000 then"yes" else "no" Yes, OCaml lets you have mutable state if you need it.
FWIW, the "just functional" language in my head is a lot closer to OCaml than Lisp. Haskell and Lisp both add a lot of "extra" stuff that's cool but (obviously) not needed to be "functional". Not that OCaml doesn't have "extra stuff", but it's not as dominant. YMMV.
Re: Why functional programming?
#46This 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?
#47I am not as experienced as you, far from it, but my -very limited - understanding of functional programming is that it's all about limiting side effects, which means limiting the ways you can screw up something. I could try to explain what I think, but John Carmack did it much better than I would ever do in a blog post, that was once submitted on HN: http://www.altdevblogaday.com/2012/04/26/functional-programm...
Re: Why functional programming?
#48Earlier 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…
It is also important that your approach be uniformly enforced: you would have to encourage people to refrain from saying that FP programmers have the same skill level as non-FP programmers, since that opinion is also not based on any evidence.
Re: Why functional programming?
#49I 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 b…
You'd only have 20mb of 'numbers' if you needed to keep a reference to the original data structure. Otherwise its' garbage.
>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
FP does not necessitate 'immutable state', even in pure languages like Haskell. What it enforces is purity, or more concrete, referential transparency. Often the easiest way to obtain referential transparency is immutable data (non destructive data structures, etc), but you can have mutable data and maintain referential transparency for high performance (see ST Monad).
Re: Why functional programming?
#50I 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 b…
This isn't that different from an imperative world. If someone somewhere has a reference to some part of that data (or may have a reference), what are you going to do? Presumably you'll make a copy. In an immutable world, adding references to an existing of data is trivial. So either you accrete more data, in which case you're in the same place as the imperative world, or it's not new data, no one is using it, and you can throw it out.
In practice it's probably more complex than that, and depends on the language implementation.
There's also something to be said for something which, implementation-wise, mutates in place when possible but semantically acts as if it is immutable.
I'm not advocating for FP in embedded systems, just trying to flesh out more detail about the trade-off here. I think even if you don't program with a language with immutability, it may be worth asking questions (which may have a simple answer in the affirmative!) about the extent to which you need mutability in a given situation vs not. "Don't optimize yet" and all that.