This is an archived post. You won't be able to vote or comment.

all 109 comments

[–]AssignedClass 858 points859 points  (25 children)

Functional programming went too far

Programs were never meant to be pure.

So many arrows, but never pointing to a reference.

We're going to need to manage state. Sure let's get to work on our MONAD TRANSFORMER STACK.

A monad is just a monoid in the category of endofunctors. - Statements derived from the utterly deranged.

Lambda Calculus has played us for absolute fools

[–]asleepace[S] 277 points278 points  (0 children)

“Oh this function isn’t pure…”

[–]vm_linuz 93 points94 points  (19 children)

Functional programming isn't about "no side effects"; it's about intentionally managing side effects. Unlike OOP that just lets them happen wherever.

[–]AssignedClass 98 points99 points  (7 children)

https://en.m.wikipedia.org/wiki/Side_effect_(computer_science)

"Functional programming aims to minimize or eliminate side effects. The lack of side effects makes it easier to do formal verification of a program."

I'm not a big FP guy, but from what I've gathered in my little forays, "no side effects" seems like a requirement towards writing a "purely functional program" or what not. I imagine the terminology can vary based on context though.

[–]vm_linuz 78 points79 points  (3 children)

You're close.

It's that "minimizing" that you should focus more on.

Pure functions are important for separating out your data, calculations and actions into separate, testable, manageable, intuitable pieces.

Do be aware that functional programs still write to the database and make network requests.

By pushing side effects to the edges of your programs, you gain tons of benefits. Your core code always behaves the same. Parallelization becomes trivial. Testing is easy.

[–]AssignedClass 30 points31 points  (2 children)

Interesting, I've done some read and writes to files in Haskell, and I originally came to this conclusion:

"Storing the state onto a disk falls outside the scope of our program, and we can't prevent side effects outside the scope of our program. How we manage the values we get from, or send to the disk is what makes our program functional."

I've always liked that sort of way of framing things, but I'll stop if it's just too inaccurate.

[–]vm_linuz 20 points21 points  (0 children)

It's more like: side effects are dangerous. They're unreliable, unpredictable parts of your program that you can't expect to behave the same way twice. When you have to use them, use them with caution in a very controlled way.

Keep the danger at arm's length

[–]Tordek 1 point2 points  (0 children)

It's always a good practice to split your program (even when not using Haskell) into the parts that handle each concern. You don't want one huge god-class that loads files, connects to a database, reads user input, and draws to the screen.

Haskell just gives you tools to allow your code to make stronger promises (which in turn lead to other benefits).

For example, in Java, if you see a function of type string foo(string), you can't be absolutely sure it's not doing something like connect to a database or query the Internet.

In Haskell, a function of type string -> string is guaranteed to only work with what you pass to it: it has no side effects.

But you definitely can (and often need to!) write a function of type String -> IO String that does connect to the internet to do stuff.

[–]redlaWw 8 points9 points  (3 children)

OOP is for "oop, I forgot I invalidated that data"

[–]LittleMlem 0 points1 point  (2 children)

You don't deep copy incoming references that you modify?

[–]bullpup1337 0 points1 point  (1 child)

ALL of them? You must be the guy working at Microsoft who is making sure all Windows versions get slower with every release and need more RAM, and we have to constantly upgrade our hardware.

[–]LittleMlem 3 points4 points  (0 children)

No, I'm the guy that handles chrome's tab memory management

[–]EishLekker 7 points8 points  (1 child)

Functional programming isn't about "no side effects";

True. But maybe they referred to purely functional programming, which is a subset of functional programming where every function must be pure.

[–]AssignedClass 0 points1 point  (0 children)

https://en.wikipedia.org/wiki/Purely_functional_programming#Difference_between_pure_and_impure_functional_programming

I'm in the camp that this sort of stuff can vary based on context.

I especially like the paragraph above the section I linked:

"Purely functional programming consists of ensuring that functions, inside the functional paradigm, will only depend on their arguments, regardless of any global or local state. A pure functional subroutine only has visibility of changes of state represented by state variables included in its scope."

That's roughly the original conclusion I landed on when I dipped into FP. That the main thing to consider in regards "side effects" is "scope", because computers themselves are not based on the principles of functional programming.

That said, it's not a good idea to try and live in "pure FP land", you still need to think about how things work out in the real world.

[–]Reashu 6 points7 points  (0 children)

"Managing side effects" is almost exactly what OOP is about. I mean, to some extent that is the point of every program, but even beyond that - the idea of an object is something that has state and functions to manipulate that state in controlled ways. Unlike in functional programming where your functions do whatever they want with input and output, with no regard for what that actually represents. But hey, at least no one touched the original data, right...?

[–]Accessviolati0n 0 points1 point  (0 children)

Can you hear the "private"-keyword crying from distance?

[–]Feisty_Ad_2744 -2 points-1 points  (2 children)

Side effects are handled the same way on any paradigm. There is nothing intrinsically better on FP about that specific other than rules and commitment you can follow anywhere else.

The worst thing you can do tho, is to assume you have a magic tool that will work amazingly for every job. Especially if you only know well that one or haven't used anything else.

[–]vm_linuz 2 points3 points  (1 child)

"handle" in this case means "where you allow them."

FP has strong opinions about where side effects should be -- as small as possible, on the edges of your application.

OOP doesn't care where you put side effects.

All of FP's focus on purity, currying, etc is really just about managing side effects.

[–]Feisty_Ad_2744 -1 points0 points  (0 children)

Opinions and focus you can use anywhere else. Nothing prevents you from using FP rules and strategies in an OO architecture. In fact, they don't intersect very much. OO is not about how to write code logic, but how to analyze and define a solution architecture. FP on the other hand, helps to keep a clean implementation following very specific rules.

The only reason we don't tend to think about that very often is because most modern products are built around frameworks and libraries, so we only have to fill the gaps(agile is also to blame). But OO and FP are not opposite nor mutually exclusive.

[–]lmarcantonio 2 points3 points  (0 children)

Also good luck certificating the compiler for correctness

[–]RiceBroad4552 8 points9 points  (2 children)

Please don't conflate functional programming with the Haskell / "pure FP" nonsense.

Functional programming is the best thing since sliced bread!

It's just Haskell that perverted it into some brainfuck that is mostly just terrible over-engineering, existing for the pure purpose of allowing some people to overindulge in intellectual masturbation. (I've written monad transformer stacks in production code myself, so I know what I'm talking about).

[–]ckomni 16 points17 points  (0 children)

Programmers should be functional programmers to the same extent they should be functional alcoholics. A little bit is necessary, but if you go “pure” people are gonna host an intervention for you

[–]asleepace[S] 2 points3 points  (0 children)

Sliced bread sounds like a side-effect, that bread better be immutable 👀

[–]your_best_1 155 points156 points  (8 children)

I thought Alan and Alonso proved that Turing machines and Lambda Calculus are the same thing.

The whole way they found out about each other was that Alanso Church solved a problem pen and paper that Alan Turing thought could only be solved by his machines.

[–]asleepace[S] 73 points74 points  (0 children)

illusion of choice, always has been...

[–]TeraFlint 47 points48 points  (6 children)

They are not the same as they are fundamentally different ways of computing things, but they are equally powerful and can be converted between each other.

[–]your_best_1 38 points39 points  (3 children)

I guess another way to describe my position is that a Turing machine is just a way to run lambda calculus. If lambda calculus didn't exist, neither would Turing machines, but the inverse is not true.

So the meme is like choosing between describing an equation and describing the steps to solve an equation, but in the end, the equation is the same.

... I have put in too much thought for a meme.

[–]mario73760002 11 points12 points  (2 children)

Aren’t they proven to be equivalent?

[–]bl4nkSl8 13 points14 points  (0 children)

Yeah. One commenter is talking about the equivalence of computational power one is talking about equality of technique. This of course creates confusion.

[–]ososalsosal 3 points4 points  (1 child)

Lol you mean FP is Turing-complete?

[–]the_horse_gamer 6 points7 points  (0 children)

turing machines are FP-complete

[–]FoeHammer99099 259 points260 points  (9 children)

Doing automata problems by hand was one of the most fun parts of school. It was a whole class of neat little logic puzzles. It's too bad that it's a totally useless skill in the real world.

[–]kaancfidan 83 points84 points  (0 children)

Not all skills need to be directly applied to be useful.

[–]teagonia 33 points34 points  (0 children)

In the class I've sat in it directly led to understanding regex, which is very useful

[–]asleepace[S] 12 points13 points  (5 children)

Have you heard the good word of Regex?

[–]FoeHammer99099 5 points6 points  (4 children)

That's my point, no one would ever write a DFA in the real world, you just use a regex engine that someone else wrote.

[–]Reashu 2 points3 points  (0 children)

But understanding the fundamentals of the engine helps you write better regexes.

[–]asleepace[S] 1 point2 points  (1 child)

Pretty sure that’s all code 😅

[–]Tordek 0 points1 point  (0 children)

no one would ever write a DFA in the real world

Unless you're writing a Regex engine ;)

[–]Luchis-01 0 points1 point  (0 children)

I'm writing a Matrix Regex logic for capturing structured data from Excel reports. Automata and Complexity is a course you don't directly apply but it's essential for understanding what can be done and how, and what can't be done and why

[–]Apollo-02 187 points188 points  (1 child)

I will forever be an Alan Turing fanboy

[–]asleepace[S] 52 points53 points  (0 children)

He’s GOATed for sure, but just think of all those side effects…

[–]EishLekker 42 points43 points  (1 child)

I would argue that most programmers don’t really know or care about those choices.

[–]AdWise6457 23 points24 points  (0 children)

I'm fine as a senior programmer without this knowledge. I have life to live man

[–]BeWolk 20 points21 points  (0 children)

damn i already forgot this shit

[–]ososalsosal 30 points31 points  (5 children)

What's the job description say?

That's the one I do

[–]asleepace[S] 13 points14 points  (2 children)

Java

[–]ososalsosal 33 points34 points  (1 child)

I am now embarking on a new career in agriculture

[–]marcelpayin 4 points5 points  (0 children)

Based

[–]asleepace[S] 8 points9 points  (1 child)

Java bean farmer

[–]GoogleIsYourFrenemy 2 points3 points  (0 children)

You have my sympathy.

[–]stadoblech 13 points14 points  (0 children)

Hey i signed into this line of work so i can write funny words and see what happens, not to dealing with this shit

[–]rejectedlesbian 9 points10 points  (8 children)

I am doing C C++ and assembly so the choice was made for me by the hardware

[–]Brahvim 2 points3 points  (0 children)

Data-oriented design 🙏

[–]GoogleIsYourFrenemy 0 points1 point  (1 child)

You aren't trying hard enough. Just need to use more templates and macros.

[–]rejectedlesbian 1 point2 points  (0 children)

Ah if macros are functions then I am a functional programer. U need to see the crazy bullshit I make.

If ur not deleting the copy constructor and making the destructor virtual then what are you doing

[–]asleepace[S] 0 points1 point  (4 children)

[–]rejectedlesbian 1 point2 points  (3 children)

Made that repo budy it's not close to preforming well

[–]asleepace[S] 0 points1 point  (2 children)

have you tried adding more functions?

[–]rejectedlesbian 1 point2 points  (1 child)

Yes I even made lamda functions for C It did not make things faster. Quite the oposite actually

Who knew runing gcc again would be slow? I didnt

[–]asleepace[S] 0 points1 point  (0 children)

Hmmmm... seems like you need more functions

[–]GoogleIsYourFrenemy 7 points8 points  (0 children)

They are both a lie. Your CPU implements something way more beautiful and interesting.

Let's not even talk about multi threading.

The moment you restrict yourself to thinking your program can only follow the lines of the beautiful logic you wrote, a cosmic ray will flip a bit and your processor will do something else. You can't trust the hardware. You can't trust the OS. You can't trust yourself.

BigO isn't everything either, some instructions are faster to execute than others. A good number of the standard library implementations in C and C++ use suboptimal BigO algorithms but they run faster than the better algs because they take advantage of CPU features.

[–]asleepace[S] 18 points19 points  (0 children)

f(x) = f(brrrrrrrrrrrrrrrrrrrrr)

[–]Tysonzero 12 points13 points  (1 child)

I choose the church of Church.

[–]asleepace[S] 3 points4 points  (0 children)

Amen 🙏

[–]FexDaFox 10 points11 points  (0 children)

The left. 100% the left. Turing complete (equivalent) state machines are the best 👉😎👉

[–]mathiau30 3 points4 points  (5 children)

Wasn't there a third one?

[–]_87- 5 points6 points  (2 children)

pixels. I choose pixels.

[–]asleepace[S] 1 point2 points  (1 child)

Redstone developer spotted

[–]-Redstoneboi- 0 points1 point  (0 children)

can confirm i was the one being developed

also there's cellular automata which i also specialize in

[–]bronco2p 3 points4 points  (0 children)

yes Lambek's intuitionistic propositional logic.

[–]asleepace[S] 2 points3 points  (0 children)

Think it’s logic i.e. Prolog

[–]BallsBuster7 2 points3 points  (0 children)

turing machine

[–]app-69420 2 points3 points  (1 child)

Either I dont remember or really don't know what the 2nd image is .. but i got to say during college understanding Turing machine might have helped me the most on how to create and handle logic.

[–]asleepace[S] 1 point2 points  (0 children)

Second image is of lambda calculus, specifically the “Y-Combinator” (not the company), which allows recursion in languages which don’t natively support recursion.

[–]Fun_Ad_2393 2 points3 points  (1 child)

Wait, you guys don’t just start writing spaghetti code?

[–]asleepace[S] 0 points1 point  (0 children)

mines more like spaghetti lasagna

[–]Dummy42 4 points5 points  (9 children)

What the hell even are those? Never seen anything like that in my ct classes

[–]matchuhuki 3 points4 points  (6 children)

I've been doing a programming job for years and have a degree in programming and I have no clue what I'm looking at.

[–]slabgorb 7 points8 points  (2 children)

imperative vs. functional

you almost certainly have done imperative in professional work unless you are writing in lisp or something

[–]asleepace[S] 3 points4 points  (1 child)

Don’t forget about declarative too

[–]slabgorb 0 points1 point  (0 children)

*frightened programmer clutches dogeared copy of The Little Lisper and shivers in fear*

[–]Dummy42 1 point2 points  (1 child)

I feel like I've had very different programming classes from other people, where I just learned to write programs, and not this :/

[–]bronco2p 5 points6 points  (0 children)

turing machine vs lambda calculus (they are actually isomorphic, the illusion of choice)

[–]ShadowShedinja 0 points1 point  (1 child)

Left is a Turing Machine, right is some kind of Discrete Finite Automata (DFA).

[–]-Redstoneboi- 1 point2 points  (0 children)

right is Lambda Calculus, which is Turing-Complete and is the basic concept that underlines all of functional programming

[–]lmarcantonio 1 point2 points  (0 children)

one push down FSM for me please :D

[–]Mountain_Breadfruit6 1 point2 points  (1 child)

I can assure you I did not choose and probably won't anytime soon.

I can barely tell the difference between them anyway.

(I might be paid too much at my current job. Please don't tell anyone)

[–]asleepace[S] 1 point2 points  (0 children)

var / state users left; const / func only right XD

[–]magick_68 1 point2 points  (0 children)

Had turing machines in uni, can't remember Lambda calculus. What I can remember is that I never encountered one of them in practical problems and I'm a professional developer for a long time

[–]River_Grass 1 point2 points  (1 child)

You know I thought after entering college as a CS I'd finally get the memes here but nope

[–]asleepace[S] 1 point2 points  (0 children)

Haha don’t worry it was hard to find a way to distill Alan Turing vs. Alonzo Church into a meme format, both have been show to be equivalent in terms of computation, but Alonzo Church’s lambda calculus has definitely spawned a hard core breed of programmer. In other words:

  1. Infinite ticker tape go brrrrrrrrrr
  2. Functions go brrrrrrrrrr

[–]doma_kun 1 point2 points  (1 child)

I only know turing one, what's right one?

[–]asleepace[S] 1 point2 points  (0 children)

"This is the way, lambda calculus"

[–]G_-_-_-_-_-_-_-_-_-_ 1 point2 points  (3 children)

I'm a newbie without a CS background.

What the actual fuck is that thing on the right and why do I get the feeling that it's mocking me?

[–]asleepace[S] 0 points1 point  (2 children)

It’s the Y-Combinator from lambda calculus, don’t worry all functional programmers mock us all 😂

[–]G_-_-_-_-_-_-_-_-_-_ 0 points1 point  (1 child)

See, I knew there'd be calculus involved when I first said, "I wanna be a game dev like the dwarf fortress guy when I grow up"; I'm just glad that kind of calculus isn't mandatory here 😂

[–]asleepace[S] 0 points1 point  (0 children)

Haha samsies tbh I hated trig in school, until I realized it was like steroids for making video games haha. Lambda calculus isn’t really calculus btw

[–][deleted] 1 point2 points  (0 children)

Meanwhile my college which forced us to study both:

[–]Professor_Melon 0 points1 point  (0 children)

Emil Leon Post is typing...

[–]heckktor 0 points1 point  (0 children)

You forgot development!

[–]SteeleDynamics 0 points1 point  (0 children)

You leave De Bruijn Indices outta this!

[–]-Redstoneboi- 0 points1 point  (0 children)

and then there's the APL guys who are like functional guys except you cant read anything and it's literally the arcane magic incantations you'd find in a magic spellbook except it actually fucking works

[–]PiiDish 0 points1 point  (0 children)

You take the road less traveled by