Hacker News new | ask | show | jobs
by DeadReckoning 3622 days ago
Why on earth are people complaining about using Javascript to illustrate functional programming language concepts? The whole point of this is helping non FP programmers to wrap their head around these terms. Javascript is by far the best mainstream language for doing functional programming.
1 comments

I'm not so sure. In C++, most certainly a mainstream language, two copies of the complex number `2 + 3i` are equal to each other. In JavaScript, they're different. (Assuming you make two objects with fields `realPart` and `imaginaryPart`.) How can you do functional programming on a foundation where not even the most basic property of equality holds (every value is equal to itself)?

Remember that functions are mappings from values from a domain, into values from a codomain. A language whose treatment of compound values is as feeble as JavaScript's can't possibly constitute the right foundation for doing functional programming.

Just define your own equality operator?

Eg in Haskell, whether things can be compared for equality depends on their representation. (Eg church encoded datatypes do not lent themselves to the built-in derivation of the == operator.)

If you can't use the builtin `==` operator, you can throw away much of the builtin functionality. The runtime uses `==` in far too many places without ability to provide your own comparator. You want to use your own types as keys in a `Map` or `WeakMap`? Yeah, not going to work.
> Just define your own equality operator?

Have fun doing that in JavaScript. Have fun making objects/hashtables use your custom equality operator when comparing field names. Have fun making sure that every single part of your program respects the notion of equality defined by your custom equality operator.

> Eg in Haskell, whether things can be compared for equality depends on their representation.

In any higher-order langauge, that's a feature, not a bug.

Not knowing anything about this, is the end effect the same?
The effect is that, if you want to reason about JavaScript programs, you can't carry out your reasoning in JavaScript itself, but rather have to use an external metalanguage - ideally something like Hoare logic, but, realistically, usually an informal, insufficiently understood, even insufficiently defined language, like English.

OTOH, a Haskeller can reason about Haskell programs by just evaluating Haskell expressions.