r/functionalprogramming • u/Voxelman • Nov 06 '22
FP Finally it clicked
I have been programming for years. But only in imperative languages like C or Python. Or more precisely, always only in imperative programming style. My beginnings go back even further to C64 Basic and 6510 Assembler.
When I wanted to learn Rust I reached my limits. My first thought was: "Why can't I change the variables? Why do I have to put 'mut' in front of everything?"
Eventually it occurred to me that Rust borrowed a lot of ideas from functional programming. So I started to look into it. I read books, I watched YouTube videos, and I tried to work through tutorials on different functional programming languages.
I basically understood what FP was about (purity, side effects), but I never understood how to implement it in a real project. Until just now.
I am currently reading the book "Mastering Functional Programming" from Packt Publishing (No advertising). I don't know if it's specifically the content of this book or just the sum of all the information from the last few months, but something clicked for me.
I think I understood the difference between imperative and declarative. I think I understood what is meant by "functional core, imperative shell".
I'm going to finish reading the book as much as I can now, and then set about finally learning Rust (and maybe even a pure functional language.
3
u/ratimetraveler Jul 11 '24
I read Grokking Functional Programming , it clicked for me
2
u/Voxelman Jul 11 '24
Another good book that helped me was Grokking Simplicity. And currently I read "Get programming with Haskell", which I also can recommend
17
u/PriorTrick Nov 06 '22
Big fan of ocaml, highly recommend
6
u/ws-ilazki Nov 07 '22
Agreed. Especially since this OCaml book is easily one of the best FP introductions one could read. It's not a pure FP language, but it's an amazing one to learn, and it strongly encourages FP style even if it allows you to do other things if you really try to.
3
u/Arshiaa001 Nov 06 '22
Suggestions for functional languages to learn: Haskell if you want to learn pure functional programming, F# or Scala if you want a language that's actually usable.
3
u/Voxelman Nov 06 '22
I looked at different functional languages. The mentioned book uses Scala. But I my final goal is to learn Rust and because functional programming is an important part of Rust (and the reason for the steep learning curve, at least in my opinion) I want to understand what is all about FP in general.
2
u/A1oso Nov 06 '22
Note that Rust is not a functional language. Functions in Rust are not usually pure, they don't allow currying, Rust doesn't use immutable/persistent data structures, it doesn't even have optimizations for tail recursion.
Trying to write Rust in a functional style would be very difficult and not a good idea. Rust is an imperative language, but one that stole ideas from a lot of different languages, including functional ones.
4
u/Voxelman Nov 06 '22
I know that Rust is not a functional language, but it really helps to learn Rust if you understand the concept of FP and why Rust is influenced by this paradigm
4
4
u/Zephos65 Nov 08 '22
My pipeline was theoretical learning of lambda Calculus -> LISP -> haskell
Lambda Calculus gave me the foundation, lisp was a good middle point between theoretical and practical, because the syntax is very similar to what lambda calc looks like in my opinion.
Haskell is the holy grail for me though because it's fully featured enough that I'd actually use it in real life. A lot easier to handle compared to LISP and is just so expressive (and fast as fuck)
1
u/Jeklah Nov 06 '22
You put mut in front of variables where the value is going to change. It's kind of the opposite idea of CONST where you put that in front of variables you know aren't going to change.
In other words variables are consts unless you put mut in front of it (mutatable)
6
u/Voxelman Nov 06 '22
You have misunderstood my statement. I already understood what " mut " is used for. I just didn't understand at first why you want immutable variables.
I didn't know functional programming and the meaning of immutable variables at that time. Until then I worked exclusively with mutable variables and even a lot with global variables, simply because I didn't know it any other way. If you then suddenly get to know a language in which you can not change variables, you ask yourself what that's all about.
1
u/long_void Nov 06 '22
Another way to learn is by using Dyon, which has a lifetime checker, but no borrow checker. In Dyon, you only need to put mut in front of arguments, so it is a little more ergonomic than in Rust, but you'll love the extra safety in Rust when maintaining libraries.
4
u/Voxelman Nov 06 '22
The problem is not so much the language itself. It's the paradigm behind the language.
0
u/Arshiaa001 Nov 06 '22
Suggestions for functional languages to learn: Haskell if you want to learn pure functional programming, F# or Scala if you want a language that's actually usable.
0
Nov 06 '22
[deleted]
3
u/Voxelman Nov 06 '22
That's true. A good knowledge of FP helps with programming in general, but pure FP might be too restrictive.
Functional core, imperative shell sounds like a good choice
16
u/mckahz Nov 06 '22
Yeah people say Rust is hard but I thought it was pretty easy because I came from Haskell, but Haskell was really hard. It's probably way easier to learn pure FP first.