r/functionalprogramming 21d ago

Question Beginner in functional programming

Hi everyone, I wanted to ask how you would start from scratch in functional programming to understand it in detail. I'm a guy who comes from the imperative paradigm and I want to delve deeper and investigate about functional programming.

I'm learning Gleam and some Erlang to understand Beam, and I'm not sure if it's a good place to start. I feel like I'm lacking some theory. Any recommendations would be appreciated.

32 Upvotes

20 comments sorted by

15

u/beders 21d ago

Throw in a Lisp for good measure.
CommonLisp is multi-paradigm, Clojure is immutable data first.
Both allow for an interactive development experience and à la carte types.
Both will rewire your brain in how to solve problems.
Clojure in particular will make you think in terms of data transformation to solve problems.

5

u/11fdriver 20d ago

+1 for Clojure, particularly. I find it such an aggressively practical language to use day-to-day for exploration and building alike. It's a very pleasant interaction with FP.

I think Common Lisp is fantastic, but I think it presents too much opportunity for imperative loopholes for someone aiming to move towards FP. I also think that Erlang has a little bit of the system side of CL already, in terms of hot-reloading, reliability, and debugging.

LFE mightn't be an awful choice when considering that they're already using BEAMlangs, but there's just not many learning resources for it. Racket might be worth it for DrRacket alone, but the lang-oriented programming will be a distraction. Guile Scheme is a nice batteries-included implementation that I like a lot, but it's a bit awkward outside of Linux. Clojure wins out, imo.

0

u/die_liebe 20d ago

I advice against using Lisp because of lack of static type checking. I also think that true higher-order programming is not possible in Lisp.

3

u/beders 20d ago

did you read my comment? Static types a la carte. You use them when you need them.

And if there's one programming language that can do higher-order programming, it is a Lisp.

3

u/11fdriver 20d ago
  • OP is currently learning Erlang, a language without compile-time static types (although it does have good type analysis tools, they are optional).

  • Higher order programming is about as possible in most Lisps as it's possible to be.

  • Clojure in particular, but also most Lisps in general, give you the ability to construct and blend types at will in a very similar way to many strictly statically typed languages.

10

u/APOS80 21d ago

I would like to suggest Ocaml, I’ve tried scheme/Erlang before and I found Ocaml to be a very nice and useful language. It’s more general purpose than Erlang.

18

u/TomosLeggett 21d ago edited 21d ago

Make a cute little toy app in Elm. Really recommend that language. It's got the simplicity of Go but it's purely functional.

I wouldn't say it's a language that'll stick with you forever, but it'll help you massively when learning the ropes of functional programming. It's a language you can pick up and learn in a week and come out fairly satisfied.

It teaches you the reducer model really well, which is a good abstraction for immutable infrastructure coming from an imperative background. The whole language is centred around a declarative abstraction of web app logic, where each event is a message, each message is a type, the state is a record called the model, the current model and the event message get slotted into an update function, that returns a new model with the computations applied, which then slots into the view function to re-render the HTML rather than mutate it.

It allows you to literally write web apps with no mutation, which reduces runtime bugs dramatically, one of the neatest things about functional programming in general.

4

u/Ok-Reindeer-8755 20d ago

Alternative, since you started with gleam already, you can try lustre, a web app framework that essentially follows the Elm architecture.

5

u/_DCtheTall_ 21d ago edited 21d ago

An Introduction To Functional Programming Lambda Calculus

This book is amazing and way more approachable than the name suggests. Starts with untyped lambda calc (just two mathematical operations) and shows how you can build data structures and implement basic arithmetic using LC as the theoretical backbone. It also teaches you some Lisp at the end.

This book teaches you the thinking behind FP and the concepts can be applied to any language which supports first-class functions. Reading this book leveled up my JavaScript significantly.

5

u/cladamski79 21d ago

You could try https://www.hica.dev/docs/hica-for-beginners/, my goal is to make programming with hica very approachable for beginners (and veterans alike), hica is a functional, expression-based language. I like learning by practise, start with small examples and then dive deeper on a thing you really want to build.

4

u/cladamski79 20d ago

I have created a dedicated page for functional programming in hica, check it out at https://www.hica.dev/docs/functional-programming/

6

u/Dazzling_Music_2411 21d ago

1) Realize that there's no more destructive assignment. A=A+1 does not happen. If you really need the value of A+1, it must be bound to a new variable, i.e. B=A+1. After that you can't make B anything else.

2) As a direct result of the above, there are no for loops. EVERYTHING repetitive HAS to be done with function calls. So start getting familiar with them. You will need to use the single linked list (a bit like a stack) for all your operations. I.e. you can add to the front, or remove from the front. That's it. Almost all data handling is like that.

3) When you've got comfortable with those two, come back and we'll discuss a) HOP and b) lazy programming - where you don't evaluate anything until it's needed. Quite amazing what you can do with that idea.

Erlang is a bit too hybrid, but OK. Use some Lisp on beam, or Elixir if you must, for a more functional experience.

Personally I would suggest you start with Scheme/Racket and the book "The Little Schemer", then "The Seasoned Schemer", but I don't know how committed you are to the BEAM ecosystem.

3

u/die_liebe 20d ago

What is your opinion on OCAML?

3

u/Dazzling_Music_2411 18d ago edited 16d ago

I'm only getting into Ocaml now, but it's fine if you're interested in language building, as I am. Very practical, makes lots of concessions to "reality". Seems like a great workhorse. That's why I chose it over Haskell. But Haskell is probably better if you want to keep things ultra-pure.

But it all depends what you want to use it for.

If you just want an introduction or a flavour, I'd go with something typeless, like Clojure or Racket.

2

u/die_liebe 18d ago

Thanks.

4

u/_mkoussaSynth 19d ago

Nobody has mentioned Elixir???

Elixir. https://elixir.hexdocs.pm/introduction.html

3

u/11fdriver 20d ago

I think Erlang and Gleam are good choices, personally. Erlang was my first functional language many years ago. They're languages that will grow with you.

Erlang is weird, but still small and readable. It's a good introduction to thinking in functional concepts like recursion, folds, higher order functions, etc, plus a very good intro to why FP is useful, e.g. concurrency and reliability. I think BEAM is a good learning environment with nice feedback and hot-reloading. Libraries are excellent, everything is super reliable and well-documented. Learn You Some Erlang For Great Good is the quintessential guide imo.

Gleam is new but great, particularly when coming from or moving towards other statically typed languages. All You Need is Data and Functions was a real eye-opener for me.

https://mckayla.blog/posts/all-you-need-is-data-and-functions.html

For learning theory, I don't think Haskell is a bad choice tbf. Haskell's not actually too impractical for real work, but it certainly shines in demos and pure FP concepts. The ceiling is high and the gradient is steep, but there are lots of great learning resources, though you might struggle to read complex code until you have quite a bit more nailed down. Tooling isn't as bad as some people say. OCaml is reasonably equivalent if you prefer the look & feel.

Clojure is a gorgeous little functional Lisp on the JVM; everybody should try lisp at some point. The emphasis on generic-ness and flexibility is a key part of why I think Clojure succeeds. Transducers would be a core concept to glance over to see the sort of thing I mean, they separate the 'doing things' code from the concerns of datastructures in an elegant way.

But imho I'd say you should take Erlang and Gleam a little bit deeper rather than trying to learn more languages at this stage.

3

u/deciomsoares 15d ago edited 15d ago

I'd add a couple of suggested readings:

  • How to Design Programs: its main idea is that function (and program) design follows from how data is modeled. In other words, function structure falls naturally (in most cases) from the structure of the data it consumes. To make this point the book takes the reader through recursive algebraic data types (not named as such if I remember correctly) and both structural and generative recursion. The book uses Racket as the driving language, but I followed it with Haskell and Lean4 and it's perfect because, even though the book uses a dynamically typed language, the emphasis on data modeling with ADTs makes the types always explicit. It was eye-opening for me, even with some years of functional programming under my fingers.

  • Domain Modeling made Functional: if you're looking for a more applied example of how to structure programs with a statically typed functional programming language (it's a F# book). It's very opinionated but it showcases how taking functions, immutability, and side effects seriously can lead to an architecture and code that is both pragmatic and easy to reason about and refactor.

3

u/TankorSmash 21d ago

If you don't mind going headfirst into a new syntax, check out Elm. It's a mini-Haskell, so you can really understand the sorts of change in perspective you'd need compared to JS. Here's a 60 line demo, and see if you can add another button to reset the counter back to 0. This is the official guide for the language that helps out.

Gleam is good too, but it's a little more complex than Elm, but the syntax is a lot friendlier if you're used to imperative langs, but getting Erlang/BEAM running is tricky, whereas Elm is just npm install -g elm or something, if you've got a Javascript dev env going.