3 ms·
To the programmer Haskell gives back a tool imperative programming took away -- equational reasoning. That's lost when you write x = x + 1, and it's something
by T_S_ 15y ago
To the programmer Haskell gives back a tool imperative programming took away -- equational reasoning. That's lost when you write x = x + 1, and it's something that got you through every math class and an essential tool to construct humankind's most powerful abstractions (i.e. design patterns).
Did you ever consider solving an equation to write some code? Haskellers sometimes do. Learn about fmap and foldr and then compare what you know to map/reduce.
- Homunculiheaded 15y agoWhat's funny is that I was talking to someone just learning to program the other day, they were learning variables so I asked them if we have int x = 4; and then x = x + 1; what is the final value of x? Their intuition was that this does not make sense since x cannot possibly equal x + 1 in this case. I suddenly realize that Haskell's picture of programming in this regard may in fact be closer to the way people reason about problem before they learn to program.
- erez 15y agoOne should hope Haskel, and other functional programming evangelist come with a better element to display. The = operator is just syntax. Pascal uses :=, you could use ## for all the compiler care (as in x ## x + 1), or I could eschew all operators and have a x.assign(x.increment()), so no math student gets confused. There's great power in the idea of single assign, and the notion of immutable objects (be it variables, or else) is getting many programmers to rethink their ideas, but saying "this is right because it's like in math" is selling the idea short, and not doing any justice to the strengths of functional programming.
- T_S_ 15y agoIt's not just a matter of syntax. It's what some people call the difference between expression (which are mathematical) and statements (which are harder to manage mathematically). Check out Hughes' well known paper or some discussions about it. <http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html> http://weblog.raganwald.com/2007/03/why-why-functional-progr...; I simply point out this feature because most people focus on language features and this is "programmer feature" that you might not think of if it wasn't pointed it out.
- silentbicycle 15y agoOr, put another way, statement-based languages vs. expression-based languages. Strictly speaking, they can be equivalent, but each pushes code in different directions by default. (And for the whole "= means mutate-variable" thing, I think "<-" is a good operator.)
- mahyarm 15y agoSqueak smalltalk has the <- character. It's actually the '_' character but displays as a one character width left arrow. You can also use :=
- dons 15y agoFor those who've not seen equational reasoning used to write programs (or do simple proofs on them), here's an example from SO this week: * http://stackoverflow.com/questions/5710078/in-haskell-performing-and-and-or-for-boolean-functions http://stackoverflow.com/questions/5710078/in-haskell-perfor... Algebra of Programming FTW! See Richard Bird (or others at Oxford) for more on this style. E.g. http://lambda-the-ultimate.org/node/1117 http://lambda-the-ultimate.org/node/1117