r/functionalprogramming • u/Voxelman • May 08 '22
Question How can I learn functional programming?
The obvious answer is: just do it. But it is not that easy for me. I'm a self-taught programmer and I have some experience in languages like C, Python and Lua, but I'm not great at all.
I have a basic idea of what FP is about, and I really want to be able to apply the concept practically, but I struggle to actually write more than a few lines (in Elm). I am having trouble getting into this topic.
I've watched some videos (e.g. from Richard Feldman and Scott Wlaschin) and read some books (e.g. Grokking Simplicity), but it still doesn't "click".
What language do you recommend (or is Elm already a good choice?), and can you recommend any other practical resources to help me make it "click" in my head?
Thanks in advance
14
u/jmhimara May 08 '22
Youtube talks by functional programmers / about functional programming are nice, but they don't really help you learn anything if you're not already familiar with FP. Elm might also not be the ideal language to learn FP because it's so specifically tailored towards Web UIs that it's almost a DSL. I'd say ML-based languages (Ocaml, F#) or Scheme are the better choices to start with.
As for learning, in my opinion, the best way is through a course structure: i.e. reading or watching organized lectures and doing homework / exercises.
You can find several free FP courses online, in Coursera or EdX for example, but my favorite one is: https://cs3110.github.io/textbook/cover.html . It's fantastically organized, easy to follow, and assumes very little knowledge from the programmer.
11
u/imihnevich May 08 '22
When I wanted to learn FP, I picked Haskell, with Haskell there is no other choice but use FP. But this is a long path
10
u/janpaul74 May 08 '22
A very short (and wrong) description of FP is: there’s no mutable state.
So try to write your code without ever changing a variable again. In JavaScript for example, only use ‘const’ and never ‘let’. Or, in Scala, ‘val’ in stead of ‘var’.
Second step: make all your functions to be pure. This means that a function only returns output based upon the parameters you supply to it, and does not depend on a shared state.
This sort of forces you to apply functional tools to your code. And take it from there.
4
u/ws-ilazki May 08 '22
I always say to just go through this Cornell book. It's about OCaml but it's an excellent introduction to FP in general, and since you're familiar with Lua and Python the syntax probably won't feel too weird to you. It's not whitespace-sensitive like Python is, but it has a similar look and feel because the expression-based nature of it means you don't have to always have
endstatements like Lua requires, and the type inference lets you avoid a lot of the boilerplate one tends to expect of statically typed languages so you can write code in a more scripting-like way, especially when learning and testing things.3
u/nmarshall23 May 08 '22
Start simple with project euler.
Every single problem is side effect free.
In the beginning it's not about solving real world problems. It's just about learning the syntax and idioms.
I really like the Project Eular problems because you don't have any of the complexity of the real world creeping in. And you can focus on just solving a mathematical problem.
I learned FP using Haskell, at the time I liked the strictness of the type system. I've never used Haskell professionally. Learning FP made me a better Java and JavaScript programmer.
2
u/cherryblossom001 May 08 '22
I was going to recommend Professor Frisby’s Mostly Adequate Guide to Functional Programming if you know JavaScript, but then I saw you already know the basics so I’m not sure how useful it would be as it spends a lot of time introducing FP, purity, currying, monads etc. Like others have said, the best way to learn how to apply these is to write a real-life application of some sorts using FP or even in a pure functional language such as Haskell.
2
u/[deleted] May 08 '22
Since you already know Python maybe this will be helpful
https://www.oreilly.com/library/view/functional-programming-in/9781492048633/
You can learn FP concepts in Python, and it will be easier than learning Scala or Haskell first.
4
u/davi_suga May 08 '22
I learned a lot from a series of videos called "Haskell for Imperative programmers"
4
u/Herku May 08 '22
I always recommend to do programming puzzles like exercism or codewars. You have to like them, and maybe you don't, then ignore this advice.
But the good thing about them is: You have a small isolated problem that you can solve within a few minutes or hours. And after you are done, you can look at solutions of other programmers. On exercism, you can even request feedback for your solution. The goal here is to learn the basics of dealing with data structures like lists, trees, maps, Maybe/Option, etc.
Only after that, it is time to look for a challenging real world problem, that interests you. This has worked well for me.
4
u/el_otro May 08 '22
I'm quite surprised no one (as far as I can tell) has mentioned Structure and Interpretation of Computer Languages — aka SICP.
It's an introductory CS book that puts FP first and foremost. It's Scheme-based, but only uses a subset of it — it won't teach you the entire language.
SICP is about a wide range of ideas, all implemented in the context of FP. It shows how to implement an OO system the FP way; how to implement the basics of a logic system; how to write an interpreter and compiler for Scheme — again, all in the context of FP, and in a single-semester course book!
It shows you for instance why referential transparency is so important, and how the pitfalls of mutation make concurrency even harder.
I'd say SICP is an outline of FP and many of the ideas behind it. if you are not a novice programmer, it will require you to think of many of the things you think you know in very different ways.
Now, it won't go all the way into FP. It won't tell how to deal with state in a referentially transparent way, for instance. But if you spend the time to work out its material it will make it obvious why you need a static type system (like that ML, OCaml, Haskell, Scala, ...) if you want to do that.
Bottom line: SICP is the best gateway drug to FP.
There is a beautiful version online here.
3
u/pthierry May 08 '22
Practice, practice, practice.
Start coding bigger and bigger software with FP and you'll come up with specific questions, and your research and people's help on those specific questions will slowly clarify for you how to do things with FP.
There are few books on good FP practices, I haven't read Real World Haskell yet but it seems good. How to Design Programs may have good things to teach too, it's written by excellent programmers and teachers, the guys behind Racket.
4
u/jherrlin May 08 '22
I would recommend to look at the "Imperative shell, functional core" pattern. Uncle Bob talks about something similar and he calls it Clean Architecture. https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
I tend to use this pattern all the time when I do Clojure. My knowledge about Haskell and Elm is limited but I think it's a common pattern there to.
I would implement Imperative shell, functional core in Python like this:
Create a module (lets call it actions.py) that only contains actions like database, network or any other IO. The functions in this module should only do actions, nothing more. This is the only module where you are allowed to have functions with IO stuff in you application.
Import this actions.py into you main module. Pass the functions from the actions modules as arguments to the functions in your main module. So you pass them down as you call stuff that needs actions. The main module is the only place where you are allowed to import the actions module.
This makes the two modules main and actions part of the imperative shell. All the other modules should be populated with only pure functions and they are part of the functional core.
This pattern has been a great help for me to grasp and implement more functional ideas.
2
u/WolfPhoenix May 08 '22
Honestly the best way to learn (the way I learned) is by working in an environment exclusive dedicated to it. When I was a Junior Dev, having my PRs picked and pulled apart was a gruelling process, but 5 years later I'm able to help coach our Junior and Mid level devs through the pillars.
I agree with a couple other commenters that it sounds like you don't have a full understanding of FP and just want to grab at a buzzword. I 5hinj the best approach to self teaching FP is to remember the principles it serves.
Easy to read, No hidden state, Idempotent.
Work on making your logic meet those 3 requirements and break things down into smaller and smaller functions and you're more than half way there.
2
u/ZigaTronUltra May 08 '22
Instead of a language to start with, I'd like to recommend a book. How to Design Programs second edition is a good beginner textbook to learn functional programming. The textbook uses tailor made teaching languages to teach programming principles and skills in a language agnostic way. These languages use prefix notation and are implemented in Racket.
The principles and skills learned in the book will transfer to other languages.
2
u/Blue_Moon_Lake May 09 '22
FP is about using functions. That's it. No classes, no
new, no methods, no throws. You can still use structs though.Then there's pure FP in which a function take at most 1 argument and do not use anything but the values in the scope.
The reason to have only 1 argument is that you can more easily chain functions in language who support it. It also forces you to think a bit more on design. You can compose things right to left, or pipe things left to right.
and
are equivalent to
Sometimes it's more readable to use one or the other.
A few examples.
Minimum of two values.
The clamp function may seem like it has 2 arguments, but it's a struct with 2 properties instead. It's allowed in FP.
Handling errors and special cases is done using structs and functions. Some languages provide syntactic sugar for the most common ones.
As you rarely use base types only, you sometimes need to use more advanced types.
Doing this would not work
A way to handle it would be
But it can quickly become cumbersome to handle all the special cases. Monads abstract all that. It makes for way less ifs in your code.
In the end you can do a clean one liner
2
u/adkalkan Dec 30 '25
Since this is a 4y old thread, could I ask how did you proceed with your FP journey, OP?
1
u/Magnivilator Jun 05 '22
SICP
1
u/sahand-javid Mar 19 '23
Ready to Master Functional Programming? Learn FP from Scratch with My Comprehensive Course on YouTube!
Honestly I believe PureScript is the best language, and learning Category Theory really helped me.
But I love TypeScript, so I forced myself to learn Functional Programming using TypeScript. I had a same challenge when I started learning FP, so I created a YouTube playlist to teach Functional Programming using TypeScript with assumption that you are completely beginner in FP but you love to get really good at it. The focus is on Functional Programming and not TypeScript. I'm adding a video every 2 week to a month.
My YouTube channel name is "Web Village Voyage" and it's at "https://www.youtube.com/@webvv".
Let me know what you think if you ever take a look.
1
u/caryoscelus May 08 '22
to understand more how "everything is a function" might work with "imperative" problems, it's good to think about types. ultimately,
mainfunction maps the state of your computer from before to after it's run (for simplicity sake let's assume input as part of initial state) . any function that writes to log takes its previous state and returns new state (besides other things it might be busy with) . etc . all state changes are explicit at some point in FPsecondly, it's helpful to think in types. with a rich (dependent) type system they became specification of what you want. by writing out precise type you already state your goal — all is left is to find inhabitant of that type (if it exists) . from (at least one of) mathematical point of view all the possible objects already exist (in a meta universe is you will) and the only goal of writing functions is to tell how to find desired object
as for practical suggestions, go radical, explore type theory, proof assistants, Agda (plfa is a good introduction even if later part might be too much for a beginner), then descend back to Haskell and whatever "practical" language you want to use
3
u/[deleted] May 09 '22
[removed] — view removed comment