DEV Community

Subbu Lakshmanan
Subbu Lakshmanan

Posted on • Edited on

Any suggestions for books to learn Functional Programming Paradigm

Hi All,

As part of our educational program in my company, we have been reading books related to engineering practices. So far we have finished the following books,

  1. Code Complete 2,
  2. The Pragmatic Programmer,
  3. Learning Agile (O'Reilly),
  4. Adaptive Code (Microsoft Press)

We are small team of developers who works on iOS(moving to Swift), Android(Java, fighting for kotlin introduction), MEAN stack for web apps & services(Javascript duh!).

Can someone recommend any good books on Functional Programming Paradigm that you read and found useful?.

A quick search on internet throws bunch of books on Scala/Haskell. Language isn't too much of a challenge, however if there's any book on Javascript that would be great.

Top comments (22)

Collapse
 
bgadrian profile image
Adrian B.G.

That is a good Q, I haven't touched the subject yet.

As a side note, JavaScript doesn't support "true" Functional Programming, as in it doesn't have the specific language constructs, methods are not prime citizens, objects are.

But you can apply some simple techniques in JS, I found a repo that has the data you are looking for, I hope it helps.

Collapse
 
buntine profile image
Andrew Buntine

Not sure where you got this from. Functions are absolutely first class citizens in Javascript.

Collapse
 
eljayadobe profile image
Eljay-Adobe

FP languages are a lot more than functions being first class citizens. Its also the semantics support, syntactic support, and core language facilities. An FP language is far more than providing Filter, Map, and Reduce.

Compare JavaScript and C++ to OCaml:

JavaScript (ES6):

const cf = (a) => (b) => (c) => (d) => { return a * b * c * d; }
const answer = cf(3)(4)(5)(6)
const paf = cf(3)(4)
const answer2 = paf(3)(4)

C++:

template<typename Function, typename... Arguments>
auto partial_application(Function function, Arguments... args) {
    return [=](auto... rest) {
        return function(args..., rest...);
    };
}
auto add6 = [](int a, int b, int c, int d, int e, int f) -> int { return a + b + c + d +e + f; }
auto pa1 = partial_application(add6, 4);
auto pa2 = partial_application(pa1, 5);
auto pax = pa2(10, 11, 12, 13);

auto c0 = curry(add6);
auto c1 = c0(4); // sort of kind of like pa1
auto c2 = c1(5); // sort of kind of like pa2
auto cx = c2(10)(11)(12)(13);
// note that c2 and pa2 are called in very different ways.
// cx and pax both equal 55.

OCaml/F#:

let cf a b c d = a * b * c * d 
let answer = cf 3 4 5 6 
let paf = cf 3 4
let answer2 = paf 3 4

But to JavaScript you'd need to add transitive immutability (as if all objects were made deep frozen at creation, by default, using value semantics like in mathematics), piping, implicit currying, implicit partial application, guaranteed tail recursion (because of immutability),

Yes, you can do FP style programming in JavaScript. And you can also do FP style programming in C, or in C++, or in Lisp. None of those languages are FP languages.

Functional-first FP languages are OCaml, F#, Haskell, Clojure, Elm.

Thread Thread
 
buntine profile image
Andrew Buntine

Thanks for the response. My qualm, however, is only with the OPs mention of Javascripts function model. I am making no claims at all regarding where Javascript sits on the functional scale. Infact, I am not claiming Javascript is even a mildly good language for any type of programming. I am simply saying that functions in Javascript are first class constructs.

Maybe I've been misunderstood, but I am lost as to why that is a controversial comment.

Thread Thread
 
eljayadobe profile image
Eljay-Adobe

You are correct. Functions in JavaScript are first class constructs.

(The OP mentioned the Functional Programming Paradigm, which JavaScript falls far short compared to functional programming languages.)

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

Kind of, in JS functions are first class objects, they can have properties and other functions attached, the difference is that this objects can be called ().

When comparing to static OOP languages JS functions are high ranked citizens, you can do crazy stuff with them, now we have lamdba functions too which is more amazing.

But in functional programming functions are first class ..anything, even the operators are functions

(++)                    :: [a] -> [a] -> [a]
[]     ++ ys            =  ys
(x:xs) ++ ys            =  x : (xs++ys)

and you can overload them

fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

and many other things I do not understand yet :)

Thread Thread
 
buntine profile image
Andrew Buntine

Functions in Javascript are first-class. Period. The ability to model them as objects with instance members is not relevant. The fact that Javascript separates the notion of operators and functions does not have any relevance either. Nor does the ES6 lambda syntax. Javascript has always had first-class anonymous functions! Semantically, it's not recent at all.

The term first class is well-defined in our field. If you look it up and test each requirement against the capabilities of functions in Javascript, you will see that they are indeed first class.

Thread Thread
 
bgadrian profile image
Adrian B.G.

Functions in Javascript are first-class. Period.

This is what I usually say too, but there is no period, if you learn more advanced topics you find out that everything in JS is an object. You cannot just say is not relevant (like you say), is very relevant on THIS topic, when you compare the language to Functional Programming ones.

JS -> everything is an object
FP -> everything is a function

You can loosely say that functions are prime citizens in JS and in FP languages, but ... we have to differentiate them to a degree, world is not white and black like you say, there are many grey nuances.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects. -- MDN documentation

Also Arrow (lambda called in other languages) functions are not anonymous functions, they have different implementations and effects. Anonymous is just a function without a name (which is not recommended anyway because it's harder to debug).

Thread Thread
 
buntine profile image
Andrew Buntine

We are talking in circles here. Your quote is saying nothing of relevance. The ability to furnish a function with properties and methods that operate upon them does not matter at all.

Let me make this really clear.

You say: "methods [Functions] are not prime [first-class] citizens"

You are wrong.

Functions in Javascript conform to all of the requirements that make a construct first-class in a programming language.

If you still want to disagree this with, please provide a concrete example demonstrating how functions are not first-class in Javascript.

Collapse
 
subbramanil profile image
Subbu Lakshmanan

Wow!! thanks for the reference. Definitely it should help. I will pass to my team to get their feedback in choosing the next book.

Yeah! I understand, Javascript isn't truly a functional programming language, however you can apply functional programming principles.

Collapse
 
shaunakpp profile image
Shaunak Pagnis • Edited

Hey, that's a good question!

If you're looking for learning FP from scratch, then I'd suggest taking a look at Structure and Interpretation of Computer Programs. It's freely available as a HTML book, or you can buy a hard copy on Amazon too. SICP teaches you to write programs in Scheme which is a dialect of Lisp.

And if you just want to brush up on FP then you can refer this awesome-functional-programming list.

Collapse
 
braydie profile image
Braydie Grove

I've been trying over the past year to shift my mindset from OO to functional and it has been tough. I'm not there 100% yet, but I can feel myself "thinking functionally" now which has been great.

I'm a .NET developer by day, so I've been looking at learning F# with a look to introducing it to the stack at work. There is a great community around F# and a wonderful learning resource is F# for Fun and Profit.

With .NET Core 2.0 released, support for F# cross-platform is also improved, so you should be able to work with it no matter what OS you're using.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I've also found FP tough. I'm using F#, and I've got 30+ years of OO (mostly in C++) that get in the way of my attempts at immersing myself in the FP paradigm.

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

I feel like books are less helpful than actually developing something with it.

If you want to stick closer to JS, then you might look at Reason. They try to keep the syntax similar to JS, and it compiles to JS while still having all the functional goodies from OCaml. I dunno any books to recommend.

Personally, I'd recommend learning Elm (also compiles to JS). There is a book coming -- Elm in Action. For now I would learn it from the language walkthrough and then expound on that with a small project. Maybe something that your team really wants that never gets prioritized. The first time you refactor, you will be blown away by how uncomplicated the process is. Make changes, fix whatever the compiler complains about, done. So it is very forgiving with changes.

Collapse
 
craser profile image
Chris Raser

Check out The Little Schemer. It's a functional programming introduction disguised as a kids' book. It's light, fun, and you can work through the whole thing in a few days. Highly recommended.

Collapse
 
eljayadobe profile image
Eljay-Adobe

If you want to stick to JavaScript as the target environment, I recommend the programming language Elm as a good FP language.

If you don't have any particular target environment in mind, I recommend F# for .NET/Mono target, and I highly recommend Dave Fancher's The Book of F#. I've done all my F# work using the Xamarin IDE on a Macintosh.

Collapse
 
evanoman profile image
Evan Oman • Edited

If you are OK with a non-JS book I would recommend Functional Programming in Scala. Scala is a nice middle ground between pragmatic languages like Java and more esoteric languages like Haskell (which to purists in either camp means that it is worthless, I would disagree).

This book specifically does a good job introducing Scala and motivating FP concepts.

Collapse
 
exadra37 profile image
Paulo Renato

What made me click in Functional Programming was the book and video course from pragdave.me/.

He really have a good way to get a OOP developer into FP, plus Elixir is a so powerful FP language and really easy to work with.

Give it a try and you will want to switch your API's and Web apps to use Phoenix framework with server side rending of HTML in Micro Seconds... yes you read it correctly ;).

Plus Elixir is the only language that I am aware that was designed from scratch to be distributed, concurrent and fault tolerant... Let it crash is the philosophy in the Elixir/Erlang ecosystem.

If you need links, just ask and I will give you a list of them :)

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Whatever book you find, don't get trapped in the idea that functional programming is somehow limited to certain languages. Pretty much all languages now have a lot of functional constructs -- they've become a staple of all programming..

You can find an overview of the various paradigms at my site, it includes functional programming.

Collapse
 
buntine profile image
Andrew Buntine

SICP is a classic text in this field.