339 points · by adamnemecek · 10y ago · 103 comments
I am happy to see these ideas work their way into any language. I just question the utility here. So there are potentially abstract things going on in my code? Great. So what? I can maybe notice it and get back to work.
Haskell gives me more. It says I can define some of the abstractions and write code against them. More realistically, it allows someone smarter than me to define the abstractions which I can then code against. For better or worse, this is expected now of even beginner Haskell programmers, where "monad" rears its head as an inbuilt type class, in inbuilt functions and some of the earliest type errors.
In Haskell, a monad isn't just an abstract concept that you can see if you squint hard enough at your code. A monad is code. It has an implementation as a typeclass, in Ocaml as a module type. How is it implemented in JS?
I am also sceptical that these abstractions are useful outside statically typed languages. Haskellers habitually ride towers of these constructs and I for one couldn't do it without a type inferencer. I already find scalaz too painful for me personally.
I now try to learn new concepts in a language I'm more familiar with, and then learn the language/syntax.
I find this repo wonderful!
Seeing examples together to me is an even stronger way of illustrating the concepts and differences between implementations.
Also, its very strange to see these concepts illustrated with Javascript. I imagine thats something like trying to learn Chinese using the roman alphabet. Not that it can't be done, but a lot of important details are necessarily missing.
That would explain why C is still so prevalent, as opposed to being 'yet another language from the 70s' ;)
On my own, if you don't come from CS or EE background you hardly will get through HR.
NB I don't have a CS or EE background.
So basically, completely normal and reasonable? (see http://languagelog.ldc.upenn.edu/nll/?p=10554)
I think you're being sincere in expressing a common attitude within the Lisp/FP community, but it's an unfortunately limiting attitude. It would appear strange to an ancient Sumerian to see the Epic of Gilgamesh translated into English, but the translation is far more accessible and thus far more influential than the original.
I'm a front-end dev and I got into Clojure and Elm because of some books/libraries for functional programming in Javascript. It's doubtful I would've ever had enough interest to learn if no one in those communities made an effort to translate some of their concepts into a language that I already use.
That's the way most front-end devs learn new things. People talk about FP attitude here, but it is no-www attitude actually.
well said!
(i'd have found es5 more accessible still.)
Chinese kids learn chinese characters by learning the roman alphabet first. Primary-school kids in China learn pinyin, which then helps them to learn chinese characters.
I will say though for the exact cases you mention, they probably should be clear why this isn't exactly the concept as described.
Alas, not all CS programs are created equal. Many skip entirely over FP because it isn't 'what the industry does'.
Indeed.
And I don't like it at all. It seems as if everybody is afraid that web developers won't understand the concepts if they are expressed with mathematical symbols instead of JavaScript functions.
I just really sorely need examples in a syntax I understand.
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.
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.)
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.
OTOH, a Haskeller can reason about Haskell programs by just evaluating Haskell expressions.
Partial Application and .bind(). Function.bind() in JS is about setting `this` for when the function is called[1].
Constant, `const`, declaration in JS governs reference reassignment. Is that the same as referential transparency? Either way the example is wrong. The following is not an invariant. `five` cannot be changed, but `john.age` can be. The object reference is constant, but not the object value.
`john.age + five === ({name: 'John', age: 30}).age + (5)`
Lazy evaluation should be called a generator if we are doing JS specific naming conventions.[2] Seems like the Haskell camp like the term Lazy Evaluation, but the Pythonthoic yield camp likes the term Generator.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The two are actually orthogonal -- generators are a control-flow structure where control keeps re-entering an existing function context and returning from it (basically, a coroutine), whereas lazy evaluation is a language-semantics choice where every value starts out as a thunk (bit of code) with enough information to compute the value, until resolved to the result.
Generators in the Python or JavaScript sense aren't really doing "lazy evaluation" in the Haskell sense, IMHO, though I can see how one sees the parallels (infinite sequences, etc). You can have the same "infinite sequence" by just defining an iterator implementation that always returns a next value, without the use of generators/coroutines.
Likewise, there are other uses for lazy evaluation than generator-like infinite sequences. For example, lazy evaluation lets you build a (finite) table of values in a memoized dynamic-programming problem, where each value is an expression that refers to some of the other values, and the lazy evaluation semantics ensure that (i) only the needed values are computed and (ii) once a value is computed, it's memoized (the thunk resolves to its result). You get demand-based computation and memoization "for free" from the language runtime.
Everything after the first parameter to .bind() sets regular parameters.
>Lazy evaluation should be called a generator if we are doing JS specific naming conventions.
They used a generator as an example of lazy evaluation. Not all generators are examples of lazy evaluation, and not all lazy evaluation is done with generators.
Agreed about const. In Javascript, const is purely about the variable binding being immutable, and not a statement about the value itself.
And if you consider `this` to be an implicit first parameter, everything makes perfect sense.
http://www.haskellforall.com/2013/02/you-could-have-invented... (The Builder Pattern, The Iterator Pattern, The Command Pattern)
Also, this may well be the thing that finally helps me understand what the crap Monads are.
It's really all about connecting types like LEGO blocks, so the important thing is to show the type signatures.
"bind" is the function required from Haskell's Monad typeclass, but not a good choice to explain monads. It's much cleaner to explain monads as functors with "join" and ("inject" aka return).
map :: (a -> b) -> (M a -> M b)
join :: M (M a) -> M a
inject :: a -> M a
With these guys you can easily compose "dirty" asymmetric operations like (readFile :: FilePath -> IO Bytes)The definition of equational reasoning is also very weak:
> When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts.
Truths about any system can be derived from the parts. What equational reasoning gives you is the ability to study the parts in isolation from each other.
The explanations I know of (all from the Haskell community) are either
1) formal, referring to ideas from category theory,
2) metaphorical, conveying the ideas by intuitions you may already have about burritos or whatever, or
3) pragmatic, focused on how and why you use them in code.
I think this last category, pioneered by byorgey's wonderful TypeClassopedia [1], is by far the most useful in teaching people about monads but it depends on observing them yourself. (Ditching the metaphors actually seemed quite radical to me at the time -- I presumed they were necessary because everyone else did.) It builds up from understanding what a Functor is, then the next abstraction up, and so on until Monads seem like an obvious idea. The same approach is used in Learn You a Haskell for Great Good [2] but you need to doing the exercises to follow the book. sigfpe's classic explanation [3] is also by example and goes into a bit more depth. It still contains exercises for the reader though :)
[1] https://wiki.haskell.org/Typeclassopedia
[2] http://learnyouahaskell.com/functors-applicative-functors-an...
[3] http://blog.sigfpe.com/2006/08/you-could-have-invented-monad...
Furthermore, the creator of Elm has a fun explanation too. Evan Czaplicki - Let's be mainstream! User focused design in Elm - Curry On https://youtu.be/oYk8CKH7OhE?t=1454
More serious computer scientist explains monads in the following. Erik Meijer: Functional Programming https://www.youtube.com/watch?v=z0N1aZ6SnBk&feature=youtu.be... and here too https://www.infoq.com/interviews/meijer-monads
Can you recommend a good one? ... I hardly have time to
write code to learn it like people say is necessary.
The _easiest_ way to learn monads is through code (in haskell they're just a tricky typeclass: http://dev.stephendiehl.com/hask/#eightfold-path-to-monad-sa...) If you don't want to write code you could learn them like a mathematician from first principles (https://en.wikipedia.org/wiki/Monad_(category_theory)), but that seems so much harder to me I'm not sure why you would.There are a couple of other links here [0] including "Don't fear the Monad" by Brian Beckman.
You could have invented monads (and maybe you already have) [1]
Lazy: I feel like the definition of lazy needs to be demonstrated in contrast to eager evaluation.
val x = {println("evaluating x"; 5)}
lazy val lazyX = {println("evaluating lazyX"; 5)}
this would print ("evaluating x) to the console. lazyX would not be evaluated because it is not invoked. Here the eager definition of x demonstrates that it is evaluated immediately. lazyZ is only evaluated when invoked.
Monads and higher kinded types. The informal vs formal definition of some of the higher kinded types is worth noting. You're either describing the most formal definitions (identity/associativity laws) or you're describing the informal defition (has flatmap, processing things in a series as described in the wikipedia article https://en.wikipedia.org/wiki/Monad_(functional_programming) ) Monad seems to be the informal definition, otherwise associativity laws should be included maybe? Eg in Scala Try is not a formal monad as the associativity laws don't hold true. https://wiki.haskell.org/Monad_laws Also why is it "of" and "chain" here? Isn't it traditionally flatmap that indicates a type might be a monad, and then the identity laws the truly confirm it? Maybe this is js land? If so, should the doc say "this is js" as flatmap is pretty descriptive of map+flatten. It's hard to talk about - It's not at all clear to me what people mean when they talk about monads either - just throwing thoughts around.
I think you should outline how Currying and Partial Application are related maybe? Eg currying is taking a function with an arg list and making it take multiple arg lists (the act of making a function take multiple arg lists), and Partial application is the application of an argument of a curried function more or less? I think the relationship between these should be highlighted possibly to sort of draw out what part of the life of a function we're talking about if trying to illuminate the subject for someone.
It's been awesome to see the sudden popularity of our humble glossary :)