Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Functors to Monads: A Story of Shapes (jle.im)
41 points by JNRowe on Nov 12, 2024 | hide | past | favorite | 7 comments


I wonder what makes functional programming languages so incredibly inaccessible and content like this blog post absolutely incomprehensible to me after a decade of programming.

I picked up c# linq, Java Streams and functional JS, the simple concepts of anonymous/lambda functions and the idea of passing around first class functions in my first job without a second thought and use all of this on a daily basis.

Anything I read about explicitly functional languages always looks like alien, academic content about math.


Probably two things:

- Pure programming means having to unlearn a lot of stuff you usually do through intermediate variables[1]. You have to express what you want without this (and it's actually freeing).

- Lazy evaluation allowed by pure programming. It means that the order of execution is not the same as you'd have in strict/eager evaluation. It can it be hard to reason about it initially, but once you get it, it can be very powerful.

[1] Not exactly true, you can still use them but they're used in a different way.


Well then, fire up your favourite IDE, write a Stream and apply a .map to it, ctrl-click into .map and read the implementation.

Is it this?

  map f     [] = []
  map f (x:xs) = f x : map f xs
Because my feelings are the opposite.


Of course, that's not actually the implementation in GHC. When you start writing code that's used everywhere, it pays off to learn and use every optimization available. And the list fusion optimizations are relevant on both sides. Consume the input with foldr, produce the output with build. But be sure to change to a more direct implementation in a later optimization phase if list fusion doesn't fire.

No matter what language you're using, things get complicated when you're using every bit of low-level knowledge you've got to eke out every tenth of a percentage point.



I'm uncertain if you think you are contradicting me or are showing others how complex the details of the real implementation are. Either way: Be sure to check out all those relevant rewrites in the RULES pragma a few lines further!


Spoil from Java: look at the implementation of ArrayList and HashMap, and be HAPPY that all those dirty tricks have been there forever, but hidden behind such a simple interface.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: