r/functionalprogramming • u/kichiDsimp • Mar 06 '24
Question New to FP, please suggest a language and a resource
So I have been learning programming for like 2 years, I have played with only imperative languages like C, Go, JS, Python and I did a course on FP but it was in python and I didn't really understand anything
Now my college break is approaching and I want to try FP, and the main reason is I love Mathematics, that's why I am learning a lot of data science these days
I need to decide two things * a language * a resource/book , I do not prefer video courses as they are very long
as I completely new to FP, I would like the resources to be beginner-friendly so that I don't get scared and run away, but the real thing I want to learn is what FP is all about and program in it, I want to broaden my thinking way
Please suggest some good books, thanks for all the help
•
u/kinow mod Mar 06 '24
Hi u/kichiDsimp
You may find some of these resources from our Wiki helpful:
2
u/kichiDsimp Mar 06 '24
I am confused in these 3 languages
* Ocaml
* Haskell
* Elixir
2
u/kichiDsimp Mar 07 '24
I have mislead, I want to learn a ML langauge that is statically typed
4
u/v3vv Mar 06 '24
Michael Clarksons CS3110 course is what got me to understand FP
16
u/lingdocs Mar 06 '24 edited Mar 06 '24
How to Design Programs https://htdp.org
If you carefully work through all the exercises it might seem a bit long and easy at points, but I promise you it will transform your way of thinking to think about solving problems and writing code in a functional way. It changed me for life.
Then https://haskell.mooc.fi is great after that.
9
u/Voxelman Mar 06 '24
Ask 10 ppl and you get 12 answers.
First you need to know if you prefer a Lisp language or from the ML family.
Then you need to know if you want dynamic or static typing.
I prefer strong statically typed ML languages and specifically F#
9
u/pihkal Mar 06 '24
Just as a side note, while I can see this being possible, Python is one of the least FP-friendly mainstream languages out there. The creator, Guido van Rossum, is not interested in FP.
7
u/lunar515 Mar 06 '24
Grokking Simplicity by Eric Normand is my recommendation. Examples are in JS but that’s just so it is accessible to a wide audience.
From there you can try a few languages and see what works for you. I personally love Clojure for its simplicity and access to JS/JVM ecosystems.
5
u/dp_42 Mar 06 '24
Scheme lisp and sicp is a good start imo. Skip the stuff that asks you to time the stuff for now, I couldn't get it to work.
4
u/justinhj Mar 06 '24
Programming in Haskell by Graham Hutton. Work through the book with ghc.
3
u/RedEyed__ Mar 06 '24
F#, https://fsharpforfunandprofit.com/
3
u/GunpowderGuy Mar 06 '24
https://github.com/stefan-hoeck/idris2-tutorial
You should try idris2. It's a Haskell inspired languages with advancements to allow for even safer code that functional programming normally does
4
u/Worldly_Dish_48 Mar 06 '24
Learn Haskell using the book called "Haskell Programming from First Principles"
4
u/makingthematrix Mar 06 '24
Scala 3 can look friendly to you if you have experience with Python. It's a language on JVM, so the libraries ecosystem is huge, as IDE you can use IntelliJ IDEA with the Scala plugin, or VS Code with the Metals plugin, and what's more, it allows you to start with writing mutable, imperative code, and only gradually learn how to do the same things in a functional way.
https://www.scala-lang.org/
As for books, I recommend this one: https://alvinalexander.com/scala/learn-scala-3-the-fast-way-book/
4
u/KyleG Mar 06 '24 edited Mar 06 '24
I always say learn FP concepts in a language you already know. There's no reason to learn a language just so you can learn a programming paradigm if you already know one that allows it.
I would say learn it in TS/JS. That's what I did. I picked up Haskell pretty easily later on (enough to make the occasional contribution to opens ource Haskell codebases) because I had less new stuff to learn (like you can't do typeclasses in TS but you can in Haskell. I also have recently done a lot in Unison, and probably will learn Scheme and Racket soon because I'm contributing to a codebase that has stuff in these languages in addition to Haskell.
But you can learn all about monads, optics, functors, applicative functors, orderables, magmas, groupoids, monoids, semigroups, lattices, etc. in JS just fine and you don't have to learn any new toolkit. You can write pure functions. You can learn about the IO monad, functional error handling, etc.
You could learn Haskell, but you'll spend a bunch of time reading about GHC, extensions, fiddlefuck around trying to get an IDE set up right, learn how to debug (which is a pain in the ARSE to do if you already know how to debug in JS by just OPENING YOUR BROWSER lol.
Cannot emphasize enough how much time you'll waste not learning FP if you insist on learning a new language first.
Edit don't let anyone scare you about monads: if you use
flatmapwith JavaScript arrays, congratulations, you understand the totality of monads because they are just flatmappables.2
u/RedEyed__ Mar 06 '24
Note about Python.
Python is very popular, which means there are 95% of courses/articles for complete beginners in programming, which makes it hard to find advanced stuff. So you will not find any FP related materials other than "how to use map/filter/reduce" 🤦
You can do many FP things in Python, but first, you need to understand FP, and the best language for me was F# and Sott Wlaschin with https://fsharpforfunandprofit.com/.
2
u/ahusby Mar 06 '24
"Functional Programming, Simplified" by Alvin Alexander. Scala is the language used in the examples. https://alvinalexander.com/scala/functional-programming-simplified-book/
2
u/sharky1337_ Mar 06 '24 edited Mar 06 '24
What about Clojure ? It is different , but has a really small syntax and having a repl makes programming really a joy . If I go back to python I am really missing it.
I also started with Erics book it is really good just buy it !
Doing fp in python does work , but for me it does not feel very good to me. It is ok , but some concepts are easy to learn in an a real fp language.
2
u/lunjon Mar 06 '24
Elixir!
2
u/Sarwen Mar 07 '24
I'll explain why below for short the best language to learn FP is Haskell. For books, Effective Haskell by Rebecca Skinner seems a nice one.
These days almost any language (Python, Java, Kotlin, C++, ...) claim to support functional programming. There are bad language to learn FP. The best ones are languages designed for it: Haskell, Scala, O'Caml (F#), Lisp (Clojure). These languages have the syntax, the tools and libraries tailored for functional programming, making it easier and more enjoyable.
The syntax is a very important aspect. Don't
2
u/Sarwen Mar 07 '24
TL;DR: The best language to learn FP is Haskell and Effective Haskell by Rebecca Skinner seems a very nice book.
These days a lot of languages (Python, Java, Kotlin, C++, ...) claim to support functional programming. They are actually very bad languages to learn FP. The reason is they where designed for other paradigms which makes functional programming in these languages a constant fight against the system, like trying to make a snake fly ;) The best languages to learn FP are those designed for it. Their syntax, tools and libraries are tailored for making FP as easy and enjoyable as possible.
If you look at what languages designed for FP have in common, they make using function as values as simple as any other values such as integers:
In a nutshell, function values should have the same level of conciseness and support than integer values. If it's harder to work with function values than integers, then the language does not support FP.
Of course, learning FP is much easier and efficient when the system is helping you. A language made for FP will teach you good FP practices and how to avoid anti-patterns. On the contrary, languages not designed with FP in mind may force you to use bad FP practices and twist your understanding of FP.
Learning a new paradigm is learning a new way of thinking. It means we need to deconstruct old habits, understand that laws we though were universal may not hold anymore and we though was insane may be the wiser thing on earth now. It's like visiting a new culture. It's always better to discover a new culture from someone who is from this culture.
This deconstruction phase is also why you may not like the syntax of well-established FP languages. We prefer what we know. In addition it takes time to understand why things work the way they do in another culture. For example, calling a function is written "f x" with a space in Haskell, O'Caml and Lisp. It may seem weird and non convenient to use "f x" instead of the popular "f(x)". But once you understand that it makes working with functional values much easier, then you start loving it!
Haskell, Scala, O'Caml (F#), Lisp and its derivatives Clojure and Racket have a very good FP support. But Scala and O'Caml also support other paradigms: imperative and object-oriented programming. Learning FP is learning how things work in FP. But FP is not a silver bullet! Some things work best with imperative style while other work best with object-oriented programming. But the way it's done in FP may still teach you very good and useful lessons.
When something is done with mutation or oo in a multi-paradigm language, it may be because imperative/oo is the best option, but it may also be because the person that implemented it loved imperative/oo, or (s)he did not know how to do it the FP way, or if the FP way is not possible in the language because of lack of advanced FP features. It's like learning English with a teacher who use English and German words in every sentence. That's not the easiest way to learn.
That's why I consider Haskell as the best language to learn FP. Its designed only for FP so the FP experience is great! It had to implement the features to get efficient and elegant FP code for tasks where imperative/oo programming is the way to go. For example, Haskell had to find a way to implement side effects, it motivated using mondas which themselves justify having higher-kinded types (HKT) in the language. O'Caml don't need an IO monad because you can just use imperative style, so there is no high incentive to implement HKT. You don't need to understand all the technical terms here, my point is that multi-paradigm languages, while being very useful for actual work, are confusing when learning.
My advice is learn FP concepts in Haskell and once you understand a concept well-enough, use it in the language you prefer. I have a very good example illustrating my point. Algebraic data types (ADT) is a very helpful concept present in most FP languages among them Haskell and Scala. In Scala, like most object-oriented languages, data is stored in classes. Scala designers could have introduced a new data structure to implement ADT, but it would not be very consistent (hello O'Caml ;) )! Instead, they generalized the concept of classes so that you can make ADT with classes. This generalization is great! It enables to have methods on ADT variants and also get pattern matching on classes! That's super useful. But when you already know classes and want to learn ADT, it just makes things confusing and harder because you don't only have to understand the new concept of ADT but also how it is encoded through a generalization of the concept of class. However, in Haskell, you have a dedicated syntax much easier to understand.
Have fun!
2
u/Evthestrike Mar 08 '24
Haskell, Learn You a Haskell for Great Good. Haskell might seem scary at the outset, but it is a pure functional language so you will be forced to learn FP concepts
2
u/Mademan1137 Mar 09 '24
For practical fp -> ocaml, book: real world ocaml
2
u/mimomomimi Mar 06 '24
R?
0
u/[deleted] Mar 06 '24
[deleted] — view deleted comment