Functional Programming Jargon
github.com
github.com
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.
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.
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.
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.
You can use Hoogle (eg https://www.stackage.org/lts-6.9/hoogle?q=%3E%3E%3D) to look them up.
It's a bit daunting at first. But most code actually only uses operators from at most a few libraries. Once you understand Monoid, Applicatives and Monads and their operators, you'll be doing OK.
There are alphabetic names for their operators. But they are not used much---working programmers prefer the symbols. Alas, learning wouldn't be much easier with them either, because most of the work is in understanding the concepts. People would complain just as much about the cryptic names as about the cryptic symbols.
(The only mainstream library that really goes crazy with the symbols is 'lens'.)
I saw one of the problems was, that I had higher math in German and some translations aren't straight forward (monoid -> halbgruppe) so I often thought things I knew were entirely new concepts.
Yes, it's a bit of a shame that some things are so abstract that we can really give it good names. For example, the operation in the monoid is just any old operation that's associative. What name do you want to call that? I don't see anything that improves on the little Kringel that we make on the black-board, or the <> that Haskell uses.
I will say though for the exact cases you mention, they probably should be clear why this isn't exactly the concept as described.
Programming languages have procedures, not functions. (Yes, even Haskell. Consider the possibility of divergence.) Functional programming is a style that:
(0) Emphasizes values over physical object identities.
(1) Emphasizes procedures that compute functions - mappings from values from a domain, into values from a codomain.
(2) Discourages distinguishing between procedures that compute the same function.
Functional languages are languages that facilitate and encourage functional programming. Does JavaScript? IMO, it doesn't even do (0) very well, which is a precondition for discussing whether it does (1) or (2) well.
> garbage collector, persistent data structures
Don't conflate “purely functional” with “persistent”. Purely functional programming can deal with ephemeral data structures just fine - using substructural types. In the other direction, there exist persistent data structures whose implementation internally uses imperative assignment.
> powerful and flexible
Those are desirable qualities in a language, but they are orthogonal to whether the language supports functional programming.
> express all of these ideas in an accessible way
JavaScript doesn't even make it easy to talk about structurally equal values. Since everything is mutable, the only notion of equality that the language will actually respect is equality of physical object identities.
Without the ability to talk (soundly) about structural value equality, you can't formulate algebraic laws (e.g., the monoid or group laws), which is the basis for equational reasoning - one of the key benefits of programming in a functional style!
Haskell the language also knows nothing at all about invariants, and the language will happily let you make something a Monad even if it doesn't obey the right laws. The culture and libraries of course take invariants quite seriously, but some aspects of the Javascript culture also take invariants quite seriously, like Promises for example.
I would call Racket a functional language. Common Lisp? Nope.
> Haskell the language also knows nothing at all about invariants
I'm aware. But I'm not talking about mechanically enforcing equational laws. I'm talking about the ability to state them in the first place. For this, you need a sufficiently rich value language, where, for example, the list [1,2,3] is always the list [1,2,3] regardless of where it resides in memory.
In JavaScript, [1,2,3] isn't a list. It's an expression that, when evaluated, constructs an object whose initial value is one particular list, but its value at another point in time might be a different list. Furthermore, if you evaluate [1,2,3] twice, you get completely different objects. Although the objects are first-class, the list values aren't, so you can't (soundly) formulate any equational laws about lists. And object identities have an equational theory so weak (“everything is equal to itself and nothing else”) that it's completely useless.
The point is that I can reason about equality of ML and Haskell expressions in ML and Haskell themselves, with a few minor extensions (e.g., the usual laws of arithmetic, applied to pure `int` expressions), whereas reasoning about equivalence of JavaScript programs requires carrying out the entire reasoning process in a separate metalanguage. The latter is obviously far more tedious, which is why programmers have largely given up on actually reasoning about JavaScript programs, preferring testing as an alternative.
> Indeed, even in Haskell, the equality operator returns a Haskell boolean but not a proposition,
Equality testing (a runtime operation, only valid for types with decidable equality) is different from propositional equality (a type constructor on its own, which can be used on any type). See https://existentialtype.wordpress.com/2011/03/15/boolean-bli... for details. Haskell doesn't have a propositional equality type constructor.
> and cannot cope with equality on functions (I guess).
Indeed, and, in any higher-order language, this is a feature, not a bug.
I do not see how you can reason about Haskell in Haskell either, as this is a programming language and not a proof language (maybe I am missing something?).
> Haskell doesn't have a propositional equality type constructor.
This is what I meant. Since with both Haskell and JavaScript you will need to define a propositional equality, in my experience this does not help that much to have a better decidable equality.
http://www.haskellforall.com/2013/12/equational-reasoning.ht...
http://www.haskellforall.com/2014/07/equational-reasoning-at...
The reasoning is entirely carried out by replacing Haskell expressions with contextually equivalent ones.
> Since with both Haskell and JavaScript you will need to define a propositional equality
No, you need much more than a propositional equality to be able to reason about JavaScript programs. If you try to use Haskell-style equational reasoning on JavaScript objects, you can only pick between your reasoning being trivial (because every object reference is only equal to references to physically the same object) or unsound! So if you want a more useful notion of program equivalence, you'll have to use a separate logic (e.g. Hoare logic) or axiomatic system (e.g. Dijkstra's predicate transformer semantics) for reasoning about imperative programs.
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.
Without a degree very few companies will bother.
In France, Germany and Switzerland one should at very least have a technical experience level and written references from past jobs.
There is also the language barrier.
Not many accept working in English, and we had some customers from well known multinationals that only upper management was willing to use it.
Besides the degree, knowing multiple languages and soft skills really helps.
So basically, completely normal and reasonable? (see http://languagelog.ldc.upenn.edu/nll/?p=10554)
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 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.
well said!
(i'd have found es5 more accessible still.)
That's the way most front-end devs learn new things. People talk about FP attitude here, but it is no-www attitude actually.
Alas, not all CS programs are created equal. Many skip entirely over FP because it isn't 'what the industry does'.
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.
OTOH, a Haskeller can reason about Haskell programs by just evaluating Haskell expressions.
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.
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.
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.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...
There are a couple of other links here [0] including "Don't fear the Monad" by Brian Beckman.
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
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 :)