Explaining The Explanation (Functional Programming And Stuff)

0. Intro đź“ş

Explaining Metaphysics to the nation—
I wish he would explain his Explanation.
— Lord Byron (from Don Juan: Dedication)

So this explanation has been a long time coming. As I cast an eye back on the posts I’ve written over the past eight years—yay, we’re closing in on our 10th anniversary—I couldn’t help but look up the very first post: It is titled Musings on Programming and, more to the matter at hand today, devoted to musings on the art and science of functional programming.

With that, I invite you to hold on to the gist of the following remark, and all will become clear as I explain (the explanation) to you:

A language that doesn’t affect the way you think about programming, is not worth knowing.
— Alan Perlis (American computer scientist)

But what’s with that interview thing, Akram? What’s up with that?“, you ask, understandably curious about the reference in the title—Explaining The Explanation (An Interview)—to an interview or something.

I knew that was coming, heh. What you’re getting this time is a tale—make that “a narrative” as we don’t spin yarns around here, at least not as much as we probably should—that’s woven around and into an interview with the fabulous and fabled Lambda.

And who—or even what—exactly might this Lambda be? To answer that question and thereby unveil the Phantom of the Opera, so to say, I turn again to the highly quotable Alan Perlis, this time when he was dedicating a foreword to a seminal book in the computer science canons:

This book is dedicated, in respect and admiration, to the spirit that lives in the computer. I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun… I hope the field of computer science never loses its sense of fun… What’s in your hands, I think and hope, is intelligence: the ability to see the machine as more than when you were first led up to it, that you can make it more.

Ladies and gentlemen, in full candor then, our guest Lambda is none other than the spirit that lives in the computer. If that strikes you as a bit meta, let me remind you that—and picking a decidedly contemporary example for you—the entire field of quantum mechanics is predicated on notions at least twice as (seemingly) bizarre, amirite?

Can you take that?

If so, and only then, in real earnest: Onward.

1. Getting Ready For Our Guest Interview â›±

If I were to pick a language to use today other than Java, it would be Scala.
— James Gosling

Taking a cue from the creator of the Java programming language—and keeping in mind his opining above on the functional programming language Scala—we dive right into the interview with Lambda, starting with the next section.

But first, you may find some background helpful to better enjoy the larger context (And we just might get around to dabbling a bit with reactive programming, if time and space permit.) You see, the world of object-oriented programming—the one most of us inhabit (most of the time) in the corporate world, and of which Java is a fine exemplar of a language which affords OO constructs—dwells in parallel with the world of functional programming, and of which Scala is a fine specimen of a language which affords functional constructs.

Imagine, though, if those two worlds—the one of object-orientation and the other of functional programming—were to collide. Boom!

Relax, though there can admittedly be awkward moments. Imagine, too, if you will, object-orientation meeting the functional style (of programming) for the first time. The silence is deafening, with both desperately trying to break the ice. Object-orientation finally plucks up the courage and, sounding a polite note of civilized diffidence, innocently asks, “Functional, I presume?”

Akram, stop already, please. Where’s that interview you were yammering about, dude?

Goodness, is it the rigors of life nowadays—or life as we know it in our COVID-19-rearranged world—that has lowered our collective patience, amirite?

At any rate, you’re boss around here. Our interview with Lambda is coming right up. (It’s my way of formulating the evolution of my own thinking inasmuch as the functional style of programming goes, aka FP, alongside, but of course, with object-orientation, aka OO)

So here we go.

Akram: Welcome to Programming Digressions, O Lambda. I’m sure our readers would love to hear what attracted you to functional programming, and to programming in general. Shall we start there?

Lambda: You see, outside of the practitioners of computer science, I am, to the vast majority of the populace, nothing more than a Greek letter—λ—one relegated to history. But to those who are in the know, I’m the one to know, and that’s for sure. For one thing, mere mention of my name conjures up visions of creating functions out of nothing. Is that cool and gravity-defying or what? What attracted me to functional programming, right, is that I got to name the absolutely indispensable and single most important thing you can do in the entire field of functional programming: creating (anonymous) functions. Outside of that—coming now to your question about what attracted me to programming in general—I happen to subscribe to the same notion which that Perlis guy, I forget his first name, had in mind when he was yammering about “the spirit that lives in the computer.” For crying out loud, I amthe spirit that lives in the computer,” just to be clear.

Akram: At one time—and jumping feet-first now into some nitty gritty stuff—the conventional wisdom was that Scala is best put to use by arranging for a codebase to be evenly split, to a first approximation anyway, between FP and OO. That used to be considered the golden rule. Your take on that?

Lambda: Wait, what? Scala is but the new new kid on the block; I would’ve thought that you might ask me questions about hoary stuff such as, you know, Lisp or something.(I hear they’ve ported Lisp to the JVM, amirite?) Anyhow, I’ve kept up with the times far better than you may have been led to believe. If you really want to know my genuine sentiments on all things non-functional, I say we throw out all that object-oriented stuff. (Insert here one palpable gasp from our awestruck audience.) Ever witnessed a desperate soul in the wee hours of the morning, pushing their working memory to its limit, glued to their computer monitor, trying to unravel a stack trace a foot long—and we’re not talking, ahem, about those oh-so delicious, foot-long Subway sandwiches—muttering under his breath about the layers of objects that had surely conspired to create that monstrosity of a stack-trace? So there. In the land of functional programming, no such problems exist. Oh no. Hey, how about one of those foot-long Subway sandwiches now? Surely you feed your guest righteously, now don’t you?

Akram: I know you have a thing for the Ocaml programming language… What makes it so special? I confess that I know more languages than I know what to do with, but isn’t Ocaml one of the ML menagerie of languages? Either way, I—and our readers, I’m sure—would love to hear more about this niche language. (They sure seem intrigued by the curiously dromedary name.)

Lambda: Ah yes, you got that one right, boy. Come closer, and listen up, because I’m not going to repeat this: You see, Ocaml indeed hail from the ML family of  functional programming languages, the other family, of course, tracing its heritage back to Lisp. What’s radically good about Ocaml—and about Scala, or about any ML family member for that matter—is that you get to find most all of your problems at compile-time. I mean, that sort of things is not going to solve your backache; but you know the kind of problems I’m talking about. (You surely don’t want your paying customers to find your problems for you, right?) Yeah, I see you nodding eagerly. Just wanted to make sure: So yes, let your compiler do all your work for you. Let the compiler be your batman. And no, not the caped one. (Looking at the quizzical expression on your face: Sigh, kids these days; they—and you—know nothing, absolutely nothing.)

Akram: Ahem, take it easy, o vaunted λ. And coming back to Scala… Do you think Scala has kept up with the times?

Lambda: I don’t know about Scala, kiddo, but I sure have kept up with the times. But I also digress. Interesting, I saw your eyes light up at the mere mention of digressing… Anyhow, I keep hearing that the upgrade to Scala 3 is where the excitement is at. They dropped the curly braces from the syntax, and the language is looking more and more like Python now. If you were to ask me, I’d prefer Python—let’s make it python, all lower-case—to have remained a member of the reptile family. But nobody asked me, and now I hear that python, or Python to be precise, is a dynamic programming language with a smattering of functional capabilities. Coming back, ahem, to a family with better pedigree—the statically-compiled ML family of languages and of which Scala is a member—Scala’s focus is now on simplicity. The creator of Scala has ruthlessly slashed down the language back to its basics. I’m hearing that it is more type-safe than ever. Now that’s music to my ears.

Akram: We had touched earlier on OCaml—the language and not the ship of the desert, the camel, heh—and, by extension, Haskell, with its mind-bending innovations such as type-classes, higher kinded types, and stuff like that. So you knew this was coming: Any thoughts on the practical uses of category theory, which, to the visual thinkers among us anyway, conjure up visions of neat boxes in which to tuck away concepts, or perhaps beachside lockers such as those pictured above—see how they went to town on the gaudy colors—each one with a brightly boxed-up category.

Lambda: Mark my words: Stick to the basics. And unless you want to get painted in a corner where your codebases need to be rewritten because no one understands it much anymore after they littered it willy nilly with category theory-inspired opaqueness, stay clear of code that’s overly tainted by category theory. The basics—functors, monads, and stuff like that—that’s all fine. Just don’t carried away. Stick to delivering a solid, maintainable product, one that’s backed by a solid, maintainable codebase, and you’ll do good. To take another example: That reliable workhorse of a programming language that is Java, it got lambdas with JDK 1.8 and it’s been off to the  proverbial races since then, amirite?

Akram: That makes a lot of sense: I can see how one can get into a scenario of, as you nicely put it, “diminishing returns.” In fact, I recall my friend Dean Wampler opining similar sentiments in his Scala book: i.e. category theory-replete code that eventually had to be rewritten because it was impossible to understand and/or maintain. So, where does that leave the practice of FP?

Lambda: Let me tell you: There’s plenty going for FP if you stick to the basics. Read my lips: Stick to the basics. (That’s one of the most elegant of patterns I could cite for you. So go forth and do creativity, laddie.)

Akram: While we’re at it, shall we drag in Prolog? (And the next thing our readers know, we’ll be chatting about the Lisp dialect that’s been ported to the JVM: Clojure! The cat’s out of the bag now, amirite? Um, more like, the cat—my feline pal pictured above—is in the box, oh my.) But I digress: Regaining focus, so we don’t lose our readers, lemme take us back to a chat you and I had on the power of patterns.

Lambda: Indeed, and speaking of patterns, it can’t hurt to check out Prolog. In fact, a google search led me to λProlog, which—a mouthful coming up—is evidently “a logic programming language featuring polymorphic typing, modular programming, and higher-order programming.” For crying out loud, did anyone even notice the imprints of my name—λ—on it, as in λProlog? Woohoo! Those guys got good taste, amirite?

Akram: Do you see FP as the ultimate antidote to lasagna-style coding?

Lambda: All this talk of food, kiddo: Get me one of those foot-long Subway sandwiches this second, and we’ll talk about this one. Yes, all such questions will be answered then, and only then.

Akram: All the world’s a stage, and I nearly forgot to ask, how remiss of me: What do you think of the actor model of programming? Is it accurate to say, as some have, that actor-based programming is nothing less than OO done right?

Lambda: In my day, acting was done by a bunch of wayward folks over at The Globe who had got banded together and commandeered by a chap named William Shakespeare. If I missed something, pray tell me. You were speaking of actors and acting, amirite?

Akram: Never you mind about actors or acting, kind sir. But I must ask you: What would be your advice to our up-and-coming programmers in terms of dipping their feet into the shimmering pool of programming? Any resources to help them swim with the best?

Lambda: Learn the Greek lexicon, especially the part having to do with λ. Just kidding. Seriously, though, learn to build programs out of functions, the purer (the functions) the better. Actually, I was just giving you a hard time earlier: that whole object-orientation thing isn’t all that bad, truth be told. Take special care, though, when those two worlds—the one of object-orientation and the other of functional programming—collide. Boom!

Akram: You stole one of my lines there, but that’s okay. And hey, I sure enjoyed our chat. I thank you. Any parting thoughts on how to build bridges into the future?

Lambda: Yes, I will begin by elaborating on an Einstein quote—”It has become appallingly obvious that our technology has exceeded our humanity”—and remind us that technology is far from the maligned two-faced Janus it’s occasionally made up to be. All the same, when Einstein speaks—more like, spoke—you better listen up, such as when he had opined there on the perils of technology. Keep that in mind in all you do: You build bridges into the future by keeping your feet firmly planted in the ground and humanizing technology along the way. Do that, and you’ve done good. (Hey, is someone chiseling my name unto posterity? Tell them they’ve got good taste.) Good luck. Yo, next time, I ain’t talking any more till you have some of those foot-long Subway sandwiches at hand. I may be a Greek letter—λ—but I still got a stomach, and it’s growling, for crying out loud. Bye. Hello, Subway delivery?

Your Comment Here!

This site uses Akismet to reduce spam. Learn how your comment data is processed.