r/functionalprogramming • u/Neither-Acadia2395 • Nov 05 '23
Question Why is functional programming so hard
Throughout my entire degree till now, I’ve been taking OOP. Now I am in a FP course and I am struggling a lot. I understand it’s almost a total different thing. But I just failed a midterm in FP in Ocaml. I swear I could’ve solved the questions with my eyes closed in OOP. What am I doing wrong, why can’t I get a grasp of it. Any tips on how I should approach studying this.
78
Upvotes
3
u/WystanH Nov 06 '23
It's just a different way to approaching the problem. Once you get the hang of it, it should actually improve your OOP work as well.
OOP, ideally, bundles state and the methods that access and mutate that state into a black box object. Within the context of that object, mutate away.
FP, um, functionally, requires any kind of mutation be done and dusted before you leave the function. Some languages really only let you mutate via passing parameters. e.g. one fold to rule them all.
Prior to the rise of FP, I saw a ton of horrid imperative work arounds in SQL based RDBMS systems. But SQL itself is declarative: functional. OOP programmer coming back to SQL from FP seem to have a better handle on things.
2
u/[deleted] Nov 07 '23
Where do you go that has FP courses?
54
u/Long_Investment7667 Nov 05 '23
It isn’t. You have to unlearn OOP because it has a narrow way to express solutions.
20
u/nderstand2grow Nov 05 '23
it’s worth learning Haskell just for FP.
19
u/mckahz Nov 06 '23
OOP and FP are just about structuring your code in different ways. When you first learn FP, code structure isn't really what you need to worry about. If you can write a function in a straightforward imperative way, then you can write the same function in an FP way, you just don't realise it yet.
Feel like you want a for loop? Use map, fold, or filter. One of those will probably do what you want. Feel like you want a while loop? Use recursion. Wanna use indices for an array? It's probably better to use a tuple, or use a different function. Wanna use mutability? Just use a function which takes the old value and gives you a new value.
Functional programming is only really superficially different from OOP, and the more you learn it, the more similarities you begin to see. The main benefit is that the constraints and language design nudge your code in a better direction, whereas the culture around OOP tends to do the opposite. But after you learn a lot about both they basically do the same thing. Segregate and minimize state, abstract, use polymorphism, data modelling, etc. FP just wears very different clothing.
Once you familiarise yourself with the syntax, and a few basic functions you realise that you can write whatever you want. Stuff like Monads and Functors just make your code a little nicer to read, but you don't need to know what they are especially in OCaml. Actually you do need to know functors, but they're a different thing in OCaml than in Haskell, which is what I'm used to.
Also FWIW I tried learning a bit of OCaml a while ago and it amazes me that people say it's easy to learn because of all the languages in the ML family that I've tried (F#, Haskell, Elm, Roc, etc.) OCaml was easily the hardest. It's type syntax is weirdly inconsistent with the rest of the language, there's way more features than the rest, Functors just made no sense to me, and the introduction on the website is one of the most long winded and useless introductions to FP that I've ever read.
FP is easy, but 90% of learning resources on it suck for most people. If you want a good introduction I'd recommend F# for fun and profit, a blog by Scott Wlaschin who's one of the best technical communicators of our age. It doesn't really matter that it's F# instead of OCaml, both languages are very similar.
7
u/pthierry Nov 05 '23
First, if you're taking a course, it should be a goal of the curriculum that you're ready for an exam when it comes. Have you done all exercises? Have you asked a TA or teacher?
Now, it's true that you usually approach problems in FP in a rather different way then with imperative programming.
There are a few key differences, a big one being that you want to make state explicit: instead of having several parts of the code that change a mutable structure, in FP, you pass updated state as an argument. For example, instead of mutating an array with new elements in a loop, you pass a list to a recursive function that pass a bigger list to the next call.
In most FP languages, pattern matching is also a big part of idiomatic code and it's usually unknown outside FP.
When I started FP, it was slow and painful to find a functional solution to a problem when it seemed easy to imagine an imperative solution. Quickly, it became easier to find a functional solution. You just need to get your mind used to this new toolbox. Starting with smaller problems and doing lots of exercises is crucial. It takes practice.
Have you tried platforms like Exercism or Codewars?
6
u/teach_cs Nov 06 '23
I don't have much to add to the excellent posts already here, but this is usually where I bring up one of my favorite programming languages quotes:
"The hardest programming paradigm to learn is your second one."
Be patient with FP. Think about how very many hours you have spent immersed in imperative, OO programming, and then be patient with yourself as well. It's not harder than the other, but for you it is hundreds (or thousands!) of hours newer. It really is a different way of thinking, and it really does take time.
Enjoy the journey!
6
u/armahillo Nov 06 '23
I dont mean this to sound cheeky, but its hard because you havent learned it yet. You havent learned it yet because you havent practiced enough (the “enough” quantity varies person to person).
Keep practicing. It will become easier.
5
u/llynglas Nov 06 '23
I came from procedural programming years ago, think punched cards and flow charts. Then Scala and C++ came along, and the switch to OOP was so tough. Theoretically it was fine, but actually thinking in OOP, that was something else. Suspect, you are experiencing the same issues with your paradym shift....
It does get better. All I can suggest is to write as much stuff, and read as much stuff in FP as possible.
4
u/ebonyseraphim Nov 07 '23
Short answer is because functional programming is actually restrictive in terms of what you can tell the machine to do. For reasons you're in the process of learning right now, it is intentionally that way and there are benefits to writing programs, and solving problems that way.
The way people practice "OOP" these days isn't really a paradigm, it's just imperative programming with restrictions on data access and a push to identify similarities in data and data processing across different, seemingly differently typed pieces of data in a system. Because of this, I don't consider the restrictions of OOP substantial enough for anyone to have real hurdles learning it. It's more about ensuring a program's code has good abstractions and you're able to reason about it, and add to it more than anything else. You aren't fundamentally being forced to solve a problem differently than imperative code through OOP.
Functional programming on the other hand, specifically removes the ability to tell the machine when to execute an instruction, and you never modify data stored in variables -- only using it to evaluate something else. This forces you to solve problems with a different structure that may not be the order you think it should be in, and it forces you to break things down differently focused on "what can I store in a variable and pass on to the next expression to evaluate?" Higher order functions are also a powerful thing letting you push further into solving more problems effectively and carries over to imperative languages too.
I don't think I have any tips for learning FP better other than realizing the restrictiveness of it is intentional. Keep pushing and work with it because it is extremely valuable. You've learned it well enough if you at least "get it" and feel self-satisfied solving problems that are well suited for it, and have done one or two things using it that are possible, but awkward, in it. In software engineering / programming, you don't truly know a language well until you're keenly aware of what the language is NOT good for.
7
u/clintecker Nov 05 '23
it’s not hard, just different
3
u/videoj Nov 06 '23
I'll point you to Scott's website F# for Fun and Profit. He has lots of articles on functional programming from the prospective of an OO programmer. Look at the list of popular posts in the left hand columns for a starting point.
3
u/TheFirstDogSix Nov 07 '23
A couple bits of advice that worked for me:
Try Elixir. Looks like an imperative language, but quickly you'll end up doing functional. It tricks your brain. 😉
Read "Functional Programming Patterns" (Bevilacqua-Linn). Shows OOP patterns and their functional equivalents. Also brain tricking.
Good luck! It's worth it. Even if you continue to use imperative languages, knowing FP will change how you code for the better!
3
u/flummox1234 Nov 07 '23 edited Nov 07 '23
If you know an OO language learn the FP parts of it, e.g. lambdas and Procs in ruby, a bunch of the lodash.js methods like map are FP-ish. It may help you understand better if you're on familiar ground.
Personally I think FP is a lot easier. It's a MASSIVELY reduced scope so there are fewer things to keep in your head and known boundary conditions, with cool things like simplified recursion, and currying.
This was a useful explanation with F# for me early into my conversion to FP. Maybe it'll help you too. https://fsharpforfunandprofit.com/rop/
5
u/FalseRegister Nov 06 '23
Idk why everyone is antagonizing OOP vs FP.
FP and OOP are not mutually exclusive. Probably the closest antagonist to FP could be imperative programming.
OOP is about modeling entities as objects. FP is about modeling code as functions. You can totally work with objects in functions.
Imperative programming is about giving instructions specifically and one by one.
Some languages even mix FP, OOP and allow imperative style writing, such as Scala.
1
u/azium Nov 05 '23
What languages are you most familiar with? Perhaps you should read a book / do tutorials about FP that are written in a more familiar language so it can sink in better.
2
u/frej Nov 06 '23
It can be harder to learn something different than from a clean slate. I.e., existing mental models, are likely incorrect and break. It's best to start from scratch, and many Universities start with FP as
- Classes/objects are extra abstractions that are added on top of functions. It is also best to ignore inheritance almost always...
- It is better to work with a strict type system first. The Ocaml type system is an excellent tutor with immediate feedback, forcing you to think about correctness. Types are there to ensure the correctness of programs, not to make writing any program easier. That is a luxury you have at a university. Don't assume that exists in the industry.
2
u/ToreroAfterOle Nov 06 '23
I really think people find FP to be challenging because most schools these days teach procedural languages like Python in their intro to programming classes. The biggest hurdle I think might be that in procedural languages the code is structured as a sequence of steps that will be executed in that exact same order, at that exact time. This is easier at first, and can result in programs that are simpler to reason about when the program is small.
Thus, a lot of people never get to see what the advantages of referential transparency (which isn't exclusive to FP, sure, but is definitely made easier by it), because they really only become apparent when the code gets larger and you have to refactor stuff.
tl;dr - I think the reason is most people being taught procedural languages as their first language coupled with a lot of them not thinking FP is worth the effort because the true advantages are difficult to convey in some small self-contained tutorial code.
2
u/maxjmartin Nov 06 '23
I don’t find much difference between OOP and FP. FP is just prefix based programming while OOP is infix based. Think of a numerical object that you want to add another object to. So a.add(b) is the same as add(a, b).
I realized that after revisiting algebraic function notation.
Edit: for add(f, b) to add(a, b)
2
u/Daisy_fungus_farmer Nov 06 '23
Grocking Simplicity by Eric Norman helped me learn FP principles. He has a video/audio podcasts too you can listen to.
2
u/dskippy Nov 07 '23
What are some of the questions that you had trouble with in FP and could do easily in OOP?
2
u/RunnyPlease Nov 07 '23
Once you get a handle on functional programming you’ll actually start finding ways to do it even on object oriented projects. It’s just such a clean way to write code, you create modular chunks of functionality that can be composed into more interesting chunks, and it is dead simple to test.
Instead of asking yourself “how do I make an object that comprises state and behavior” ask yourself “What if I just had a function to do the thing I need?”
Just keep at it. You’ll get the feeling for it and it will be like achieving enlightenment.
2
u/radioactiveoctopi Nov 07 '23
Empty your mind and accept the new paradigm….
2
u/Voxelman Nov 07 '23
It's not hard. It's just different (from imperative programming).
It's like switching from Windows to Linux. Or learning Dvorak after 10 years of qwerty. They do the same thing, but in a different way. And it can be hard to learn the new way.
2
u/TheMerovingian Nov 07 '23
Wait until you find out that OOP is somewhat controversial, widely misunderstood, not universally practical, or even a good solution for most problems. I find functional thinking to be much more practical in the long run.
OOP is good at maintaining the state of an object, but terrible because it mixes data and logic. The idea was that objects are smart and pass messages to other objects, but you always end up with god objects that own everything. If a concept is hard to explain and understand, maybe the core idea is flawed.
Functional programming is really simple: build small functions that do simple things, have no side effects, and compose them to do bigger things. I like building functions to give names to decisions for if statements. I rarely use else, elseif, or switch statements because of that too.
2
u/Useful_Space_9099 Nov 07 '23
Agree with others it’s a different way of thinking. Here is a video (with inflammatory title/content) but might help blast you into FP thinking.
Not here to cause trouble, I found a lot of value in this video and hope you do too.
https://youtu.be/QM1iUe6IofM?si=XUZ3x30hFmb9IHiw
2
u/Low_Strength5576 Nov 07 '23
Functional and procedural programming are different. You just need to learn both completely separately.
2
u/[deleted] Nov 07 '23
I agree with everyone else. OOP is much harder. I forget the psychological term, but we often conflate something as easy AFTER we learn it and forget the struggle to learn it. And the fact that you are learning FP afterwards means that your entire understanding of what programming is is based on OOP way of thinking.
Try to pretend like you are learning to think about programming without relating it to what you already know when studying FP. It will get easier, much easier.
2
u/endianess Nov 08 '23
You sound exactly like me at University but thirty years ago. Since then I've never had to use it or even come close. If it's an option can you drop the class and do something else?
2
u/no_brains101 Nov 08 '23
Because it's very different. I only knew java and kotlin and I wanted to swap to Nix. It's been like a month now and I'm starting to like it, but it still can be hard. I had to unlearn some things, and learn some other things, and when I go back to kotlin I'm probably going to inadvisably use way too many maps because the function composing thing is pretty fun. I also really like that you can create a function with 4 arguments, give it 2 arguments, and the rest of it is still a function.
2
u/[deleted] Dec 21 '23
Haskell is papa bear's bed--too hard. Clojure is mama bear's bed. Just right. FP is only perceived as hard because people think it's all or nothing, papa bear's bed. You don't need all of it, just a dash.
Also, don't think of FP as opposing OOP. I use both all the time in the same programs. FP is a paradigm, a way of teasing the pure part of a program into its own area. And doing this makes things waaaaaay easier.
Start with ClojureScript (or Elm). Write a trivial app or game.
I guarantee when you get it, the necessary parts of FP, not the whole kit and caboodle, you will see that it is far and away easier than what you once did when all you understood was OOP.
2
u/Emanu1674 Jul 22 '25
Because it's bad. How people use a language where having I/O (the only thing that makes programming actually useful) is wrong is beyond me.
(This short rant is brought to you by the Procedural Programming Gang)
2
u/gc3 Nov 07 '23
Oops is about putting states in objects rather than global variables everywhere.
In a large complex oop program you still can have horrible state related bugs.
Fp is about not having state. So if you call a function with a parameter there are no side effects. No hidden state kept in sone object somewhere. Given the same input parameters you will always get the same result.
This let's you reason about the code, and do esoteric things.
Pure functional programming is hard, but getting rid of state is usually a winning strategy, so the habits of Fp are good.
2
u/MrJCraft Nov 08 '23
when I first started learning FP its extremely different, I would recommend practicing by invention, that is what helps me, might not help you but I would give it a try.
when I try to understand a concept I first try to invent it even if I fail in my invention I will now understand the problem domain much better because I stumbled upon many of the same solutions as the official one, until I run into a problem I cant solve then I re attempt learning it the normal way and I have a lot more background knowledge and I understand it much better. might not work for everyone but it works for me.
second I would recommend learning the different Ideas separately.
etc...I learned these Ideas from different languages where they were more prominent, or they did it differently, example in some Lisps you can define a type by a predicate, in Coq/haskell I didn't understand it until after learning how Lisp predicates work. now I mostly think of types as predicates defined by code, and the language is mathematical logic.
2
u/AxelLuktarGott Nov 06 '23
As others have pointed out it's a probably that you have to unlearn things that you now think are natural but beginners find nonsensical, e.g.
x = x + 1. People who are used to OOP often struggle a lot with higher order functions but I think they would find it super easy if they just thought of it as an object with a single method being passed to a function.But as much as I love FP I think there are lots of patterns and abstractions being used in FP that are harder to wrap your head around. "An object is a thing with a set of functions connected to it" is a reasonably easy to concept to grasp compared to "a monad is an monoid in the category of endofunctors".
With all that said I think the abstractions used in FP are better because of the level of abstraction that they use. The fact that they come from math instead of being analogies to things in your every day life makes them much more robust and makes it harder to think that they are things that they aren't.
1
u/recurse_x Nov 07 '23
Whatever you do don’t look the monad in the eyes.
1
u/alexelcu Nov 06 '23 edited Nov 06 '23
The problem might be the language, which poses extra challenges (e.g., new syntax to learn), or the learning material.
When you say OOP, you probably mean imperative programming because OOP (from an airplane view) is just a way to structure your programs such that you have (subtyping or row) polymorphism and encapsulation. You can have programs using both OOP and FP, although OOP tends to favor a different programming style (encapsulation vs. parametricity), but that rarely comes up in the kind of problems that beginners are supposed to solve as part of a course.
OCaml has modules, which are pretty cool, and very similar in spirit to OOP classes, especially in a more potent language like Scala.
The pain you're feeling is probably about describing algorithms that you'd normally describe with loops and variables. Loops are completely equivalent with tail-recursive functions, which are the norm in OCaml. Quite literally, you can take a loop and translate it into a tail-recursive function directly (all the vars become function parameters and that's it). The other pain you're feeling is that in OCaml you have many built-in functions that describe common loop patterns (e.g., `map`), but that just requires more exercise to get used to it.
One of the best books on functional programming is this one, although it uses Scala instead of OCaml (I'm recommending it because it's really good, and I don't know what else to recommend): https://www.manning.com/books/functional-programming-in-scala-second-edition
Don't give up, it gets easier. Practice more, or seek other learning materials that may be more suitable for you.
0
u/phillipcarter2 Nov 07 '23
Have you actually studied and practiced, or did you just assume you could pass a midterm with your eyes closed?