r/functionalprogramming • u/Pestilentio • Jul 17 '24
Question Learning FP - Currently at an impasse.
TL;DR: Some 4 months into studying FP through Haskell and feelling it's maybe the wrong tool to stick with after some point. What are you opinions in more moden FP tools like Elixir, Gleam?
Edit: Thanks for the responses guys, I really appreciate this community. What stuck with me is to devote more time into Haskell and try to understand it deeper before trying anything else, which is something that I pretty much expected. There are definitely many interesting tools out there, but, for now at least, I'll stick with understanding Haskell before jumping to more stuff. š
Hello everyone. First of all I want to say that this subreddit has been more than helpful each time I've posted here - either sharing my journey through learning FP, or just asking questions. I've been programming for almost 10 years now, and this year I decided that I'm gonna give a serious shot to understanding Functional Programming. I don't know if I'm ever going to switch to writing exclusively FP and I don't care to be honest, I just know/feel that it definitely reserves attention.
I'm currenly at an impasse in my learning journey and thought it could be a good idea to post here, since I've often found quite knowledgable individuals lurking.
I've completed the Haskell Book within the span of 45 days. I've done every excercise. I've also written a JSON parser for practices as well as a basic web app using scotty. I've also briefly played postgres-simple and how to handle database connections, transactions and such.
After doing the above, I feel I'm at a point in which I need a challenging project in order to make use and really understand Haskell primitives and why they are important.
However, working with Haskell, despite its elegance, leaves me with a bad taste quite often. To add a disclaimer here, I'm a person that's more on the practical side of programming, rather than doing programming for the experience of programming. While I enjoy learning new thigs, I want to learn them in order to apply them. It's fine for me to learn a bunch of unapllicable stuff as well, but I do tend to filter them out as time progresses. I already work full time and I want to make use of my time outside work when studying in an efficient way.
As Chris Latner recently mentioned it feels like Haskell was not designed for modern computers, which is probably true. Also, following Simon Payton Jones, it seems Haskell was also not designed for production purposes. There are many many things you have to set aside in order to work and learn Haskell. In the recent years there have been amazing progress in the tool of popular/rising ecosystems, like Golang, Rust and I dare say JavaScript, that Haskell seems to lack thereof. I'm sure you can create anything with Haskell despite the difficutlies, but, when correlated with other tooling, it seems to underdeliver in terms of experience.
Having recently worked quite a bit with Go, I feel that it's probably the best imperative language we have right now, in terms of production value. It has an amazing tooling, it pretty simple, it has very solid multithreading primitives and it's really fast. I did some matrix multiplications in Haskell/Go/JavaScript and the results of Haskell were really really bad. I'm sure I missed quite a bunch of compiler optimizations but it seems that performance was never a priority for Haskell.
What I've come to believe is the problem with imperative languages though is that they tend to create enormous mudballs of code, regardless of the simplicity of the language. Imperative programming seems that it does not scale after some point. Declarative systems, even though inherintly more complex, they will eventually outscale imperative ones and perform better in maintainance and extensibility long-term. This is the area where FP seems to have production value to me.
What seems intersting in,which I haven't spent time yet, is the Erlang ecosystem. Erlang seems to be a product of a production need, rather than an academic one, and I expect that it's oriented towards solving problems rather than proving statements. Diving into erlang with Gleam or Elixir is something I would do if I have a problem that justifies the complexity of using BEAM.
I don't know how to continue from here on. I have doubts regarding Haskell and it beeing a solid choice for a modern development. I feel like modern tools designed for modern computers and today's developer needs tend to feel more natural that 20-30 year old projects/languages. When writing Haskell I feel more like I'm fighting the language rather than learning with it.
I want to get a better grasp of Functional Programming. Regardless if its Elixir, Ocaml, Gleam etc, when seeing those languages now I can already correlate ideas from Haskell, most of the time, which feels amazing. But I know deep in me that that's not enough. I want to really understand FP and come at a position when knowledge is transferable to any language, functional or not.
To sum up, my problems continuing with Haskell are:
- Clunky tooling regarding LSPs, debuggers, profiling
- Slow compile times
- Pretty bad performance when related to other languages
Personally, I feel that the above are part of the reason modern tools and languages get people excited quite often. They address long lived issues that are tough to deal with in environments that are 20-30+ years old. There's definitely merit in modern tools that people create.
Regardin this post as a whole, I would be curious about your opinion on this. Do you have any other languages/tools you'd advise me to look at, instead of Haskell? Or maybe you believe Haskell is definitely worth the "burden".
Do you have any projects that you did and helped you level up your understanding in FP?
Thanks for reading till the end!
8
u/RobertKerans Jul 17 '24
I work with Elixir (so caveat that this comment is going to refer to that rather than Erlang; Erlang has quite a lot of warts that Elixir smooths over or removes, and the OOtB dev tooling [Mix] is much better). And yeah it's absolutely about production need. It's not theoretical or experimental, it's extremely practical. It being FP is an implementation detail. Syntax/semantics-wise, the precursor is Prolog rather than ML, but syntax is probably the least interesting thing about it - it's kind of a minor detail, the VM and the OTP libraries are the key parts.
It is for building reliable soft-realtime systems: say you have a telephone switch (might be a big exchange in a city, might be a box halfway up a mountain). Takes a crapton of concurrent incoming calls, routes them onwards. Systems have to track all of those calls in realtime + have timers for each one, report and handle errors for each one. If one fails, or if part of the system fails, that hasn't got to affect the rest of the system at all. If the entire switch fails, it has to be back up and running immediately. Needs to be able to run on a potato. Needs to be able to be updated while the system is running without taking the system down.
You get all the tools to program this OOtB. And the model works really well for similar domains to telephony: ie web, IOT.
Gleam I've vaguely played around with - I looked at it a few years ago, then it seemed to die, then 1.0 arrived with a load of interest. And it seems fine? I just don't really see the point ATM: the entire reason I want to use a BEAM language is for the BEAM, to give me tools to make concurrent programming a bit easier, and there seems to be very little focus on that in the gleam docs and blurb. But I'll just keep an eye on it and see where it's at in a few years.
Re Elixir, language is essentially complete, still has warts but it's IMO overall excellent. If you just want to get a feel, then I'd recommend Elixir in Action, pacing is great, steadily introduces the platform features chapter by chapter.
7
u/GovernmentMammoth676 Jul 17 '24
If it's a mixture of FP and practicality you're looking for, I'd suggest checking out FP oriented Scala, leveraging tools like cats-effect and/or ZIO. These both offer their own flavors of pure FP in Scala, but since the language itself is a multi-paradigm JVM language, you are left with more flexibility from the compiler if/when you want it. Tooling support is fairly mature, and since it's primarily a JVM language (though can also target JS and native), you get interop with Java.
In my experience, FP Scala has offered a great balance between capitalizing on the benefits of FP while maintaining a reasonable level of pragmatism in real-world development and access to a rich and mature ecosystem (JVM).
6
u/miyakohouou Jul 17 '24
I hear you when you're talking about the issues you've had with Haskell, sometimes languages just don't land with people, but I've been using Haskell professionally for a long time and I think it really can be a good and effective choice for industrial use-cases.
I don't want to sound too much like a shill, but if you're willing to spend some time with a second Haskell book, I wrote Effective Haskell and it's very focused on learning Haskell for practical purposes and touches on things like performance tuning and includes some more practical focused exercises.
5
u/tesilab Jul 19 '24
The world *needs* Haskell, not to implement products in, but to get some fundamental ideas straight. Programmers are not exactly mathematicians, but they would benefit from recognizing which parts of their programs are mathematically tractable, and strive for a recognizable purely functional layer, rather than sprinkling all the bits of math somewhere inside a mutating/side-effect ridden ball of mud.
I would even go so far to say that where possible, you should maintain a functional "reference implementation" of some algorithms you know will need to be implemented imperatively in production. You can more easily verify the correctness of the reference implementation, and maintain it to compare the optimal version against.
So go forth, program in something practical, but apply the functional lessons for better stratified, more mathematically tractable, declarative-where-practical approach.
I'm curious to know what you meant by the "complexity" of using Beam?
4
u/MonAaraj Jul 17 '24 edited Jul 17 '24
Gleam should probably not be looked at as a modern language, as it does not have optimizations, for example. It is quite a basic toy project that I don't believe is ready in any way for production software.
About your matrix multiplication project, you should have asked in the communities like here on Reddit, Discourse, Stackoverflow, Matrix, or Discord. There's many knowledgeable people who know what you did wrong, and Haskell performance can very easily be improved by making small adjustments to idiomatic code, most of the time.
One of my Haskell projects is an interpreter for a programming language that uses the ROSE paper's system for row types and has basic hindley milner inference. I also previously had an IRC server implementation in Rust and I am rewriting that in Haskell.
Haskell is not too commonly used in projects, but nevertheless there are some well known projects that use Haskell. If you know about the SimpleX chat, or the Wire chat app, both of those are major users of Haskell. There's also hledger, pandoc, shellcheck, the aura package manager, NASA uses Haskell for some things, and github's syntax highlighting.
The bit about the tooling being a bit awkward is true, but luckily enough it was much, much worse, before. I know that it doesn't seem like that's lucky, but it means that our tooling can improve -- and it does improve, it's just at a bit of a slow pace. Personally, I have not noticed GHC being slow at compiling at all, it's definitely faster than C++ or Rust for me, and honestly I never cared about compilation speed in the first place, as it has never been so slow as to hinder me. As of right now, the tooling is definitely at a very usable place. It is way more convenient than python or nodejs to me, and while it is a little bit lacking in some areas compared to Rust, it is not unusably so. Eventually, you learn the knobs you need to learn when it comes to doing what you want by reading the GHC user guide, the cabal docs (and even though it might look like it doesn't contain what you need, still read the Nix tab), and asking around in the community.
Personally, I think that the issue you're having is that you spent too little time actually working on your own Haskell projects, which could be a reason why you're feeling awkward in the language. As you make more projects and experiment with implementations more, abstractions and performance, you get a better handle for what's going on. It is especially helpful to ask other people in the Haskell community, because there are many that are very knowledgeable.
I think I personally had a similar experience, and I think that most beginners fall into the trap of tutorial hell, like with monads as an example. You commonly see people scouring monad tutorials to get intuition for them, but the way to get a proper handle on monads isnt to read someone else's explanation, it is to use monads yourself. This is a similar situation: getting more use in Haskell will give you more intuition about how you could write something, how to improve performance, etc.
4
u/Panda_966 Jul 17 '24
I share the sentiment but donāt really have any advice, as Iām actually kind of following your footsteps ( reading Haskell book right now, planning on writing a parser and maybe scotty projectā¦).
However, I'm following roc-lang.org with interest. There are a couple of good talks and podcasts from itās author Richard Feldman about it. Itās similar to Elm but for backend/cli apps and explicitly focused on performance and fast compilation. Itās in very early development, however.
2
u/pthierry Jul 18 '24
It's interesting because your feelings contradict both Haskell's design goals and my own experience. The first desgin goal of Haskell was (cf. A History of Haskell: Being Lazy With Class):
And now that I'm really comfortable with Haskell, it's my goto language for anything practical. I really think they succeeded on this whole design goal. My team uses exclusively Haskell to implement web services (and now that GHC has JS and WASM backends, we might switch from Elm to Haskell for the front-ends too).
Quick side note: the moment in Chris Latner's discussion with Primagen doesn't mention Haskell, only Ocaml and Lisp, and I don't know if he knows that, but what he says wouldn't be true for Haskell in many cases. I know it's not true in many cases for Common Lisp either, as it has notions such as specialized arrays in its specification to enable unboxing (and if you
(declaim (optimize (speed 3))), a compiler like SBCL will warn you when it couldn't unbox).As for Haskell, 15 years ago, Gill and Hutton described the worker/wrapper pattern to transform code in such a way that it uses unboxed data in its core logic and only the result is potentially boxed at the end. It's a transformation that GHC can do automatically in some places (cf.
-fcpr-anal).I would also note that it's always better to start writing a correct and readable implementation and only optimize for low-level stuff if there's a measured need. Remember:
So if you wonder if Haskell can be practical, we are many developers to find it extremely practical and it has been designed that way.
Which leaves the important question: what leaves you a bad taste when using Haskell?
I think Haskell is a great vehicle to learn FP because it prevents you from half-assing FP: there's no side effects in functions, everything is immutable. It's actually what enables Haskell to do things that no other language can, like having a safe STM (other languages have copied Haskell's STM, but theirs cannot be safe and you must rely on developer discipline to avoid having side effects in the STM code).
But this also means that Haskell can be more of a struggle at some points in your learning path, because it's uncompromising. Me and a friend of mine both made the observation that as we learned Haskell on our own, it basically took each of us 3 tries to "get" Haskell and really start using it for projects. When I teach Haskell to junior devs in a professional setting, we don't see that difficulty because they learn with the help of more experienced developers to help navigate the difficult spots.
So maybe we could discuss the specific pain points you currently have with Haskell? They may be something you can easily get past.
6
u/dave_mays Jul 17 '24
I'm not one of those "quite knowledgeable individuals" and don't even have a CS degree. I studied manufacturing engineering and want a system that helps me write reliable code, especially with my lack of CS background. I've been drawn to FP, but also to the supposed reliability of Rust. I like the idea of getting pretty good at a language I can apply both client and server side to reduce context switching and reuse data structures. I'd also like something that helps me out on some of the really hard stuff I don't think I'm smart enough to do correctly like concurrency. So considering all that, I'm trying to learn Gleam and its front end UI library Lustre. While not strictly FP I also like what I've seen in Dioxus, and wonder a bit if modern imperitive is just as reliable, though this doesn't give you the BEAM like Gleam does.
(Though I also find myself drawn to Clojure, but get worried about maintainability while reading arguments on both sides of the religious battle between compile time types vs dynamic typing).
6
u/KyleG Jul 17 '24 edited Jul 17 '24
Try Unison. The tooling is amazing, and the docs are first-class. And it uses abilities instead of monads (though the standard monads do exist as well) to handle IO and exceptions, so the code is easier to grasp if you're not used to monadic-style programming.
I've been writing a ton in the language recently bc it's such a joy. Don't let the landing page pitch as a "distributed language" confuse you: it's a general-purpose one, but just makes distributed computing easier since support for that is baked into the language via a feature called abilities.
The way I explain abilities is that it's kind of a blend of a class, an interface, and the reader monad.
You use abilities in your code the way you'd reference functions that are part of an interface, and then at the top level you provide a handler, which is like an implementation of the interface.
So for example you might have an SMTP ability with functions like
send : Email -> ()andreceive: '[Email]and you code everywhere using those, and then at the top level you inject an implementation viaprovide Smtp.handler.gmail username password '(all your code that does SMTP stuff)and it magically weaves in a Gmail-related implementation of SMTP into your downstream code.If later you decide your SMTP should be implemented via some other handler (maybe one written to do SendGrid bulk emails) you'd just swap out your handler implementation for a different one and all your downstream SMTP code switches over to that. Like the reader monad grabbing the environment and performing actions with it.
6
u/dr_bbr Jul 17 '24
Came from vb/c#, so I went to learn F#. Idk what all the difficult FP words mean but turns out I don't need to. It's my every day language. It's functional first but you can do something like mutation if you want to. Really love F#, can highly recommend.
3
u/stdmemswap Jul 17 '24
I led three huge projects that owes to FP for their manageability. All are in TS and a bit of Rust.
I like how u/MysteriousTrove pointed out that FP is a paradigm; it goes across languages despite their limitations.
Aspects that translates well to those langs are first-class function, higher-order functions, pure functions, immutability, monad, declarative/expression-based, and some concurrency tricks in FP. Surprisingly, DOD works well with FP too. I also adopt Rust's ownership to TS.
There's a cost to adopting this approach. Modules needs to be categorized by patterns instead of domain; domain linking uses strictly the module's filename. Example: all parsers for all business domains are collected in a folder because it is often a pure function that returns either a parsed object or a parse error. Another example, within a folder is a constructor/factory, etc which can own some data, another folder host constructs that cannot own data (due to the absence of
mutin TS)Somehow knowledge dependency pattern emerges, one which naturally prevents cyclical dependency for those languages that allows it.
Some aspects, for example recursions are overrated. It's a nice thing to learn but it doesn't scale well in complexity. For example, operations that involve manual scheduling and/or control yielding will eventually need recursions to be converted into an active object.
3
u/yawaramin Jul 18 '24
I feel like the obvious answer to your issues is OCamlāit has fast compile times, and is a simpler and more pragmatic FP language. I often describe OCaml as the midpoint between Haskell and Go. Check out my post to get a quick overview with some sample (real) code: https://dev.to/yawaramin/practical-ocaml-314j
3
u/ToreroAfterOle Jul 18 '24
I personally started my FP journey in earnest with Scala, but I've more recently been exploring Elixir and OCaml and find a lot of the knowledge transfers (recursion, pattern-matching, higher order functions, immutability, algebraic data types, errors as values, etc). You really can't go wrong with any of them. Whichever one you choose, I strongly suggest you join a Discord or find experienced mentors to learn from, though.
I initially tried to teach myself Haskell in a "vacuum" by just following a book and failed miserably and only got discouraged. With Scala it didn't go so well either in the very beginning until I found more experienced devs to learn from (well that and also buckling down and going through some of RockTheJVM's courses). But YMMV!
re: Tooling - can't speak for Elixir or OCaml, but it's common sentiment that the tooling has gotten quite good for both. I don't have a lot of experience with Haskell so I can't comment (I do plan on revisiting, though!). I can comment on Scala 3's tooling and, while it had a rocky start, it's a lot better now. I mainly use coursier, sbt, and either neovim or vscode + Metals and rarely encounter any issues these days. If you find sbt to be unfriendly, have a look at Mill instead.
2
u/[deleted] Jul 17 '24
Haskell-language-server has never given me any issues
2
u/grimonce Jul 17 '24
One big problem is build time and dependency management during these builds... I recently tried learning yesod using an older notebook (it has 6th Gen Intel chip). And it never finished building the hello world example.
I'm only half joking
2
u/[deleted] Jul 18 '24
Four months in ten years and Haskell is just too much hair-shirt? Why is functional programming a thing for you? What is the lure of the lambda? What are your objectives? If Haskell hadnāt already piqued your interest and enthusiasm enough that you donāt already know the answers to these questions then Iād find something else to do. Really.
1
u/Complex-Bug7353 Jul 28 '24
I agree with a lot there. The only actual decent-looking Haskell projects I've been able to make with relative ease are TUIs using the Brick library/framework.
3
u/[deleted] Jul 17 '24
[removed] ā view removed comment
2
u/[deleted] Jul 18 '24
[removed] ā view removed comment
8
u/[deleted] Jul 17 '24
[removed] ā view removed comment