Live data from Hacker News

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

news.ycombinator.com

1–10 of 112 posts

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

#1
When I read academic articles or blog posts written by functional programmers it feels alien to me because of the terms used in their writing. I am not just talking about monads, monoids etc, but the general programming terminology itself is different to what I regularly hear and read as imperative OO programmer. For e.g. this sentence "abstraction over type constructors"; whenever programmers from C++, Java etc world will read that they are sure to say, "eh?!?" and fall off their chair.

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

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

#2
I found it equally tough because while there are a lot of resources to cover the basics and get you coding in Haskell there isn’t much to bridge the middle ground from there to the level the deep enthusiasts and experts are at. All I can say is keep trying and reach our for help on IRC channels or at a local functional programming meetup (and by local these days you could remote in to any meetup you like!). They’ll be people happy to help.

It’s been a while since I’ve dabbled with Haskell but I believe type constructors take a type and return a type (like List takes Int and returns List Int) so an abstraction over them might be an m where say m is a monad and it takes an a and gives you m a. m is an abstraction because it could be anything. A List, or Either, or State etc.

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

#3
You're not alone. Even functional programmers struggle to read those papers. I found that in the functional programming world there's a lot of concepts being thrown around with different names.

I think your best option is to directly contact authors or very good educators and ask them exactly what they mean.

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

#5
As with so many things in life, a lot of it boils down to practice.

Are you just trying to read academic articles and blog posts? Have you tried working in one or more of the languages? Beyond "toy problems" to trying to solve larger problems?

The more you work in a language, the more you might start to understand the needs that underlie things like category theory (the mathematics of monoids, semigroups, monads, etc) and where other higher level abstractions start to play in how you want/need to write software.

There's also places to practice even in C++, Java, etc. A lot of functional programming is making its way "stealth" into even the most imperative OO languages. Are you handy with your languages map/filter/reduce libraries around iterables? Have you tried pushing that deeper towards other monads such as query transformation (Linq) or time (async/await, ReactiveX)? Have you thought about how those work under the hood? (Explored what a iterable generator or an async/await decompile to?) Have you started yet to think in terms of high level "lambdas", places where functions themselves can be reused by other objects/functions? (Beyond "callbacks" towards thinking of functions themselves as lego bricks to build with.)

There's also middle ground languages such as F# or Clojure that you can use side-by-side (in some circumstances) C# or Java (respectively) code where maybe you can't work 100% in a functional language on a project, but you can get a compromise mix.

So much of understanding the "lingo" is recognizing the patterns and/or problems to which they refer, and a lot of the easiest way you will pick up patterns and see recurring patterns is practice.

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

#6
I think the first step here is to put your expectations into perspective. Functional programming also is a range of complexity. If you're not used to functional programming and try to read articles targeted at the high-end you'll naturally struggle to understand them.

I still remember "not getting" OO in university in my first lecture. I had programmed BASIC, C, Pascal, PHP before. And if someone would have tried to explain me the idea by means of the visitor pattern, it would surely have made me say "eh?!?" too. So allow yourself to start slowly.

I'm also still dabbling in functional programming - at least that's what it feels like reading about monoids etc - but by actually using functional programming languages in my more serious spare time projects I feel I get a better and better understanding. And "those" articles start to speak more to me.

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

#7
I don't think this is different in OOP literature, where you may read something like "abstract factory pattern", you just need to get familiar with the terminology. I would start reading Pierce's Types and Programming Languages and then any book on Haskell (you may also want to reinforce your abstract algebra :)

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

#8
Most of the jargon denotes straightforward definitions. Pick the top-most frequent three terms, e.g. monad, monoid, applicative, write down the definition + a small example on a card, lather rinse repeat a few times and you're golden.

Edit. For example, let's take 'catamorphism'. Read the Wikipedia entry [0] and it's a seemingly never ending jungle of more and more abstract terms. Homomorphism, initial algebra, F-algebra, endomporphism. This easy, let's see what an F-algebra is [1] and we'll be on our way soon. "If C is a category, and F: C → C is an endofunctor of C, then an F-algebra is a tuple (A, α), where A is an object of C and α is a C-morphism F(A) → A.". Oh my God, this is hopeless.

Or, find a good description, with examples [2]. A 'catamorphism' card, abbreviated from [2]:

Catamorphism

A function that “collapses” a recursive type into a new value based on its structure. Visitor pattern.

Define a sum type Gift:

    type Book = {title: string; price: decimal}
    
    type Gift =
        | Book of Book
        | Boxed of Gift
Define a 'description' function over Gift:

    let rec description gift =
        match gift with 
        | Book book -> 
            sprintf "'%s'" book.title 
        | Boxed innerGift -> 
            sprintf "%s in a box" (description innerGift) 
    
Parametrize the function, creating a 'catamorphism':

    let rec cataGift fBook fBox gift =
        match gift with 
        | Book book -> 
            fBook book
        | Boxed innerGift -> 
            let innerGiftResult = cataGift fBook fBox innerGift
            fBox innerGiftResult

Refactor your 'description' function to use a 'catamorphism':

    let descriptionUsingCata gift =
        let fBook (book:Book) = 
            sprintf "'%s'" book.title 
        let fBox innerText = 
            sprintf "%s in a box" innerText
        // call the catamorphism
        cataGift fBook fBox gift

[0] https://en.wikipedia.org/wiki/Catamorphism

[1] https://en.wikipedia.org/wiki/F-algebra

[2] https://fsharpforfunandprofit.com/posts/recursive-types-and-...

Post reply on HN