Live data from Hacker News

Ask HN: How to be fluent in functional language speak?

news.ycombinator.com

81–90 of 112 posts

Re: Ask HN: How to be fluent in functional language speak?

#81

Earlier quoted context omitted.

Giving descriptive names just harms intuition building in the long run. I've been writing Haskell for over 5 years and to this day when I see , , >>= in my code I don't substitute English words for them.

> Giving descriptive names just harms intuition building in the long run. Citation needed

(In my experience)

Re: Ask HN: How to be fluent in functional language speak?

#82
post #38

Earlier quoted context omitted.

> We've "abstracted (the function map) over the type constructor (of the data structure being mapped over)". Then why didn't they just call those types "Mappables"?

I think this is a great question because it tends to come up a lot. What do you call the following common operations? - 1 + 0 - 1 * 1 - "string" ++ "" - [1, 2, 3] ++ [] As far as I know there is no term for this grouping of operations outside of functional programming languages which use category theory. We call it a "monoid" based on the following precise definition: > In abstract algebra, a branch of mathematics, a…

concat?

Re: Ask HN: How to be fluent in functional language speak?

#83
post #82
post #38

Earlier quoted context omitted.

I think this is a great question because it tends to come up a lot. What do you call the following common operations? - 1 + 0 - 1 * 1 - "string" ++ "" - [1, 2, 3] ++ [] As far as I know there is no term for this grouping of operations outside of functional programming languages which use category theory. We call it a "monoid" based on the following precise definition: > In abstract algebra, a branch of mathematics, a…

concat?

I can't think of any way for 1 * 1 to be concatenation.

I think the point GP was making is that a monoid has something to do with a function f that takes two arguments a and b where there is a single value for b such that f(a, special_value) == a. I think.

As someone who doesn't really understand FP concepts I would probably call those functions "functions that can sensibly be used in a call to array.reduce"

Re: Ask HN: How to be fluent in functional language speak?

#84
post #38

Earlier quoted context omitted.

> We've "abstracted (the function map) over the type constructor (of the data structure being mapped over)". Then why didn't they just call those types "Mappables"?

I think this is a great question because it tends to come up a lot. What do you call the following common operations? - 1 + 0 - 1 * 1 - "string" ++ "" - [1, 2, 3] ++ [] As far as I know there is no term for this grouping of operations outside of functional programming languages which use category theory. We call it a "monoid" based on the following precise definition: > In abstract algebra, a branch of mathematics, a…

"binary operators with an identity value"

Re: Ask HN: How to be fluent in functional language speak?

#85
post #84
post #38

Earlier quoted context omitted.

I think this is a great question because it tends to come up a lot. What do you call the following common operations? - 1 + 0 - 1 * 1 - "string" ++ "" - [1, 2, 3] ++ [] As far as I know there is no term for this grouping of operations outside of functional programming languages which use category theory. We call it a "monoid" based on the following precise definition: > In abstract algebra, a branch of mathematics, a…

"binary operators with an identity value"

the binary operation should also be associative (e.g. subtraction is no good)

Re: Ask HN: How to be fluent in functional language speak?

#86
post #73

Earlier quoted context omitted.

Giving descriptive names just harms intuition building in the long run. I've been writing Haskell for over 5 years and to this day when I see , , >>= in my code I don't substitute English words for them.

Was about to complain you can't google them, but I tried it and it worked. Huh. I guess you can google symbols now, even traditionally escapey-ones. News to me. mentalModel.update(). The query `python @` fails pretty hard though.

For Haskell stuff, you can go one better and use Hoogle: https://hoogle.haskell.org/?hoogle=%28%3C%24%3E%29&scope=set...

Re: Ask HN: How to be fluent in functional language speak?

#87
post #9

Try a textbook? e.g. Types and Programming Languages by Benjamin C. Pierce.

I agree that TaPL is an excellent book, but I did not find it useful in explaining functional programming idioms. (It does do a great job of explaining typed lambda calculus, however.)

Re: Ask HN: How to be fluent in functional language speak?

#88
If you are coming from imperative land (more specifically C++) then nothing beats articles and videos by Dr. Bartosz Milewski:

https://bartoszmilewski.com/2014/10/28/category-theory-for-p...

https://www.youtube.com/watch?v=I8LbkfSSR58&list=PLbgaMIhjbm...

https://www.youtube.com/playlist?list=PLbgaMIhjbmElia1eCEZNv...

https://www.youtube.com/playlist?list=PLbgaMIhjbmEn64WVX4B08...

Re: Ask HN: How to be fluent in functional language speak?

#89
post #82

Earlier quoted context omitted.

concat?

I can't think of any way for 1 * 1 to be concatenation. I think the point GP was making is that a monoid has something to do with a function f that takes two arguments a and b where there is a single value for b such that f(a, special_value) == a. I think. As someone who doesn't really understand FP concepts I would probably call those functions "functions that can sensibly be used in a call to array.reduce"

It doesn't need to have a perfect metaphor for all possible cases, just something you can picture in an instant: 'behaves like a concating variable length box sequences'. It's a mnemonic / learning aid, not a formal spec. But I can stretch it :)

+: concat marbles sequence

* : concat prime factors sequence

++: concat box sequence

For * , it can also be reduced to + via logarithms. I just don't have a good metaphor for '+ over reals' this morning.

Re: Ask HN: How to be fluent in functional language speak?

#90
> So how does a programmer from non-functional world become fluent in understanding sentence such as "abstraction over type constructors"?

We already have very primitive (and restrictive) imperative OO languages which are good at teaching the basics to new programmers. My observation is, FP lacks such gateway languages. Even the simplest FP-first language is intimidating.

So my answer to your question is, a good strategy to learn these can be carving out a relatively small portion from a language (i.e Haskell with only Algebraic Types and Pattern Matching) and working only on it for a period of time. Doing that doesn't teach terminology but exposes what Haskell is really obsessed about. For the curious, it is expressing type relationships as flexibly as numeric arithmetic we know from high-school.

Just like high-school arithmetic, several recurring patterns (often called formulas in Math classes) have their special names. Almost all programming languages are good at closely matching that syntax in their numeric expressions. It is unfortunately not the case for types though. In programming,

this functional type (pseudo code):

  A = (X + Y) | Z
tend to be expressed as:

  interface class A {
  
  }
  
  
  class A1 implements A {
    X x;
    Y y;
  }

  class A2 implements B {
    Z z;
  }
or as:

  union A {
    struct A1 {
      X x;
      Y y;
    } a1;

    struct A1 {
      Z;
    } a2;
  }
In both examples, it is longer to write the same "meaning" in OOP than FP, especially when there are abstract methods involved.

This difference in verbosity (aesthetically subjective) bugs an FP programmer more and more as they get used to their language. When that happens, people start naming things. Having an abstract type of `Collection` is no longer enough because we realize `Collection`, `Mappable`, `Traversable`, `Generator`, `Optional`, `Atomic`, `Pair` and `Tuple` all share the similarity of:

* wrapping a type,

* having a way to access the wrapped value(s),

* and the wish to support all operations the wrapped value supports without an unwrap, composable out of the box.

We call this pattern a name, the infamous "Functor" (incorrectly but who cares), and axiomize those assumptions formally in the target language, so that it is longer reimplemented again and again. The word "Functor" itself otherwise has no meaning, it is letter salad.

To learn those names effectively, you must be comfortable enough with the basics so that you get annoyed by the repetition and start researching new names as you stumble upon with new patterns.

In Haskell, "abstraction over type constructors" usually means (in OO terms) "declare a new generic interface and implement that in the abstract type so that you don't have to reimplement it non-genericly in the concrete type".

Post reply on HN