Latest / CyberSecurity Summary / Get Programming with Haskell
Transcript
- 0:00Speaker 1: You know, usually when you give someone directions to your house,
- 0:02Speaker 1: you're basically just handing them this really fragile sequence of steps.
- 0:07Speaker 2: Right, yeah, like go three blocks, turn left.
- 0:10Speaker 1: Exactly, turn left at the light, stop at the red house,
- 0:12Speaker 1: which you know works perfectly fine.
- 0:14Speaker 2: Assuming the world remains entirely static.
- 0:16Speaker 1: Sure, right, But like if a single road is closed
- 0:21Speaker 1: or they just miss step three, right, then the rest
- 0:24Speaker 1: of the steps are completely meaningless. They're just totally lost.
- 0:27Speaker 2: And when sequential directions fail on a road trip, I mean,
- 0:31Speaker 2: you just take a wrong turn, right, But when sequential
- 0:33Speaker 2: instructions fail, and say a banking system or an airplane's navigation.
- 0:38Speaker 1: Software, it's a catastrophe.
- 0:40Speaker 2: Exactly, it's a massive failure. But then if you look
- 0:43Speaker 2: at how a map actually works.
- 0:44Speaker 1: A map doesn't tell you what to do, right.
- 0:46Speaker 2: It just describes what the landscape actually is.
- 0:49Speaker 1: It's just a pure definition of space. It's totally immune
- 0:53Speaker 1: to you messing up step four because well there are
- 0:56Speaker 1: no steps. It's just truth.
- 0:57Speaker 2: Yeah, And that shift, the shift from executing mechanical instructions
- 1:02Speaker 2: to evaluating pure logic, that is arguably the most powerful
- 1:06Speaker 2: way to safeguard complex.
- 1:08Speaker 1: Systems, which is exactly what we are getting into today.
- 1:11Speaker 1: Welcome to the deep dive. Today we are looking at
- 1:14Speaker 1: Will Kurt's book, Get Programming.
- 1:16Speaker 2: With Haskell, and it is a fascinating book.
- 1:19Speaker 1: It really is. And the goal for this deep dive
- 1:22Speaker 1: it isn't just to learn the syntax of Haskell. The
- 1:25Speaker 1: mission here is to fundamentally demystify functional programming.
- 1:29Speaker 2: Yeah, to discover how thinking about computation mathematically rather than
- 1:34Speaker 2: just you know, giving a computer a list of instructions,
- 1:37Speaker 2: can just completely change how you understand software exactly.
- 1:40Speaker 1: So for you listening right now, whether you are a
- 1:43Speaker 1: totally experienced programmer or just someone who is like insanely
- 1:46Speaker 1: curious about how reliable systems are built, this exploration is.
- 1:49Speaker 2: Going to give you a completely new perspective on problem solving.
- 1:52Speaker 1: It really will. So Okay, before we get into the
- 1:55Speaker 1: crazy abstract math of functional programming, let's ground this in reality,
- 1:59Speaker 1: the idea. Le's talk about the environment where this actually happens. Right,
- 2:02Speaker 1: Will Kurt introduces what he calls the Haskell platform.
- 2:07Speaker 2: Which is basically a batteries included setup.
- 2:10Speaker 1: Right, you get two main tools out of the box.
- 2:12Speaker 1: There's GHC, which is the Glasgow Haskell compiler, and then GHCI.
- 2:18Speaker 2: Which is the interactive interpreter.
- 2:19Speaker 1: Yeah, and you know, in most languages I've worked with,
- 2:22Speaker 1: a compiler is basically just a.
- 2:23Speaker 2: Spell checker pretty much.
- 2:25Speaker 1: Yeah, Like, if your code compiles, it just means you
- 2:27Speaker 1: didn't forget a semi colon somewhere. Yeah, it definitely does
- 2:31Speaker 1: not mean the program won't immediately crash the second a
- 2:35Speaker 1: user clicks a button. Oh.
- 2:36Speaker 2: Absolutely, But Haskell it flips that dynamic entirely.
- 2:39Speaker 1: It really does.
- 2:40Speaker 2: There's actually a famous adage in the functional programming world,
- 2:42Speaker 2: which is, if it compiles, it.
- 2:44Speaker 1: Works, which sounds impossible.
- 2:46Speaker 2: It does. But the GHC compiler is notoriously strict. It's
- 2:51Speaker 2: almost almost hostile toward anything that violates mathematical purity.
- 2:56Speaker 1: Right, Like in a normal language, if you have a bug,
- 2:59Speaker 1: your immediate instance does just ad hoc debugget Oh.
- 3:02Speaker 2: Yeah, you just throw a print statement into the middle of.
- 3:04Speaker 1: A function, exactly the classicconsole dot log here, just to
- 3:09Speaker 1: see if the code even reach that line. You run it,
- 3:12Speaker 1: tweak it, try again.
- 3:13Speaker 2: Well, Haskell aggressively rejects that you literally cannot just slap
- 3:17Speaker 2: a print statement into the middle of a mathematical function.
- 3:20Speaker 1: Because printing text to a screen means altering the outside world.
- 3:24Speaker 2: Right, Yes, a physical monitor has to change its pixels.
- 3:27Speaker 2: In Haskell, that is considered a side effect.
- 3:30Speaker 1: It violates the purity of the function exactly.
- 3:33Speaker 2: The compiler will literally refuse to build your program.
- 3:36Speaker 1: It honestly sounds like working with this incredibly strict but
- 3:39Speaker 1: brilliant editor like it might be super frustrating at first
- 3:43Speaker 1: because you can't just slap a rough draft together.
- 3:45Speaker 2: Hey really can't.
- 3:46Speaker 1: But when that editor finally approves it, you know it's flawless.
- 3:50Speaker 1: There are zero plot hole.
- 3:51Speaker 2: And that demanding editor it forces you to completely change
- 3:54Speaker 2: your workflow. Kurt actually demonstrates this with a really great
- 3:57Speaker 2: refactoring example.
- 3:59Speaker 1: Oh the email template script.
- 4:01Speaker 2: Yes, so imagine you need a script to draft a
- 4:04Speaker 2: thank you email. Right, It asks for a recipient, a
- 4:07Speaker 2: book title, and an author.
- 4:09Speaker 1: Most programmers would just write one massive block of.
- 4:12Speaker 2: Code for that, right, like a big, messy main function.
- 4:15Speaker 2: It asks for the inputs, glues the text together, and
- 4:18Speaker 2: spits it out all at once.
- 4:19Speaker 1: But Haskell's compiler won't let you mix that pure text
- 4:22Speaker 1: generation with the impure side effect of asking a user
- 4:25Speaker 1: for input on the fly.
- 4:26Speaker 2: Right, So you're forced to break it apart.
- 4:28Speaker 1: You have to build like a toe part for the greeting,
- 4:31Speaker 1: a body part for the message, and a from part
- 4:33Speaker 1: for the sign off.
- 4:34Speaker 2: You totally isolate the pure math from the messy real world.
- 4:38Speaker 1: And because you have GHCI the interactive environment, you can
- 4:42Speaker 1: actually load those isolated pure pieces and just test them
- 4:45Speaker 1: individually exactly.
- 4:47Speaker 2: You prove that the logic of the body part is
- 4:49Speaker 2: flawless in a total vacuum, and then you just snap
- 4:52Speaker 2: all the modular pieces together later.
- 4:54Speaker 1: It's so clean. But okay, if the compiler is acting
- 4:57Speaker 1: as this ruthless gatekeeper for par logic, which it is,
- 5:01Speaker 1: we need to talk about what it's actually guarding, because
- 5:03Speaker 1: to write this flawless code you kind of have to
- 5:05Speaker 1: unlearn what a function actually is.
- 5:08Speaker 2: You really do, a function in Haskell operates in a
- 5:11Speaker 2: completely different universe than say a function in Python or see.
- 5:15Speaker 1: Right, and the divergence starts. What Back in the nineteen thirties.
- 5:18Speaker 2: Yeah, long before modern computers even existed, Alonzo Church developed
- 5:23Speaker 2: this system of formal logic called lambda calculus.
- 5:26Speaker 1: Using only functions in variable right.
- 5:28Speaker 2: He was basically trying to define what is mathematically computable
- 5:32Speaker 2: in the abstract.
- 5:33Speaker 1: But around the exact same time, Alan Turing was inventing
- 5:37Speaker 1: his theoretical Turing machine, right.
- 5:39Speaker 2: Which led to the famous Church Turing thesis.
- 5:42Speaker 1: Right. Okay, explain that because that's huge.
- 5:44Speaker 2: So Turing proved that his machine could compute anything computable
- 5:49Speaker 2: using physical sequential steps, you know, moving a tape, reading
- 5:52Speaker 2: a symbol, changing a state, very mechanical exactly. But Church
- 5:56Speaker 2: proved that his lambda calculus could do the exact same thing,
- 5:59Speaker 2: but purely through mathematical evaluation.
- 6:02Speaker 1: Without any physical states changing at all.
- 6:04Speaker 2: Right, no moving parts, just pure math.
- 6:07Speaker 1: But Turing kind of won the historical coin toss when
- 6:10Speaker 1: it came to hardware, didn't he. I mean, the chips
- 6:12Speaker 1: powering our laptops and phones are built on the von
- 6:16Speaker 1: Neuman architecture, Yes, which is essentially the physical realization of
- 6:20Speaker 1: Turing's machine. It's totally mechanical.
- 6:22Speaker 2: You take a piece of data, you allocate a physical
- 6:25Speaker 2: memory slot in the RAM, put the data in, change it,
- 6:29Speaker 2: and move it somewhere else.
- 6:30Speaker 1: Right. So traditional languages like Java or see they're designed
- 6:34Speaker 1: to give programmers direct control over that mutation.
- 6:37Speaker 2: Yes, But functional programming, which is rooted in churches lamb
- 6:41Speaker 2: to calculus. It completely abandons that von Neumann model.
- 6:45Speaker 1: It provides this universal mathematical model of computation that just
- 6:48Speaker 1: abstracts the hardware away entirely.
- 6:51Speaker 2: You weren't micromanaging memory slots anymore. You are evaluating expressions.
- 6:54Speaker 1: Okay, But and I know the listeners wondering this, the
- 6:57Speaker 1: physical CPU on my desk is still a von Neumann machine,
- 7:00Speaker 1: it is. Yeah, So how does pure math run on
- 7:03Speaker 1: a machine that's quite literally designed for sequential memory mutation.
- 7:08Speaker 2: Well, that is the magic of the GC compiler. It
- 7:11Speaker 2: acts as the bridge.
- 7:12Speaker 1: Oh.
- 7:13Speaker 2: Interesting, You write the pure mathematical logic and you never
- 7:16Speaker 2: think about memory allocation. Then the compiler takes that pure
- 7:20Speaker 2: logic and does all the heavy lifting.
- 7:22Speaker 1: It translates it into the machine code exactly.
- 7:25Speaker 2: It turns it into the highly efficient mechanical instructions that
- 7:28Speaker 2: the CPU actually needs.
- 7:29Speaker 1: So it takes the burden of state management away from
- 7:32Speaker 1: the flawed human and gives it to an infallible algorithm.
- 7:36Speaker 2: Precisely, and because it operates as pure math, Haskell forces
- 7:40Speaker 2: all your functions to obey three unbreakable rules. Right, Rule one,
- 7:44Speaker 2: they must take an argument.
- 7:45Speaker 1: Well two they must return a value.
- 7:48Speaker 2: And rule three, and this is the big one. The
- 7:50Speaker 2: same argument must always return the exact same value.
- 7:54Speaker 1: Yes, referential transparency. If I pass the number two into
- 7:57Speaker 1: a function and it returns four, it mus return four
- 8:01Speaker 1: every single time.
- 8:02Speaker 2: From now until the heat death of the universe.
- 8:04Speaker 1: Literally, yes, okay.
- 8:05Speaker 2: I have to push back here on behalf of everyone listening.
- 8:07Speaker 2: I understand the math. But this is where traditional programmers
- 8:10Speaker 2: just lose their minds.
- 8:10Speaker 1: Oh, I know. If a function always does the exact
- 8:13Speaker 1: same thing and variables can never change. Yeah, wait, how
- 8:17Speaker 1: do you program anything dynamic?
- 8:18Speaker 2: It's a huge mental shift.
- 8:20Speaker 1: Like if I'm building a video game and Mario cleex
- 8:23Speaker 1: a cooin, his score goes from zero to one. In
- 8:26Speaker 1: any normal language, I just write score zero and then
- 8:28Speaker 1: I tick it up with score plus plus or score
- 8:30Speaker 1: plus one. But if I write score zero and then
- 8:33Speaker 1: try to make score one in a compiled hash school program,
- 8:36Speaker 1: what actually happens.
- 8:38Speaker 2: Compiler throws a fatal air, just crashes it, refuse it
- 8:40Speaker 2: to compile. Yeah, because in Haskell, variables aren't actually variable,
- 8:44Speaker 2: they are definitions.
- 8:45Speaker 1: Okay.
- 8:45Speaker 2: If you define score zero score is zero. That is
- 8:48Speaker 2: a mathematical fact within that that scope. It cannot suddenly
- 8:52Speaker 2: become one any more than the number five can suddenly
- 8:55Speaker 2: become six.
- 8:55Speaker 1: Wait then, how does Mario ever get a coin?
- 8:57Speaker 2: Well, instead of mutating the existing memory slow that contains
- 9:00Speaker 2: the zero, you pass the current state the zero into
- 9:04Speaker 2: a function that returns an entirely new state. Oh wow,
- 9:07Speaker 2: you generate a totally do world where Mario has one coin.
- 9:10Speaker 2: You are never destroying or altering the past. You're just
- 9:13Speaker 2: continuously evaluating the next frame of reality.
- 9:16Speaker 1: I mean that sounds incredibly resource intensive.
- 9:19Speaker 2: It forces you to think differently, yes, but underneath the
- 9:22Speaker 2: compiler optimizes it. You aren't actually copying the entire universe
- 9:26Speaker 2: in RAM every millisecond.
- 9:27Speaker 1: Right, But why go through all that trouble, Like why
- 9:30Speaker 1: abandon something as simple as score plus plus.
- 9:32Speaker 2: Y Because mutating variables in place creates what we call
- 9:35Speaker 2: hidden state, and hidden states are the root cause of
- 9:39Speaker 2: almost every catastrophic software bug in existence.
- 9:42Speaker 1: Ah okay, And the source material highlights this perfectly with
- 9:46Speaker 1: a really terrifying test using the standard dot reverse command.
- 9:50Speaker 2: Yes, this is such a good example.
- 9:51Speaker 1: So you take a simple list of numbers one, two, three,
- 9:54Speaker 1: and you call dot reverse on it in three different languages, Ruby, Python,
- 9:58Speaker 1: and JavaScript. You would obviously expect the exact same result
- 10:01Speaker 1: from all three You're right.
- 10:02Speaker 2: You'd think. So you run the exact same code on
- 10:05Speaker 2: the exact same list, But watch what happens. Ruby evaluates
- 10:09Speaker 2: it and returns a brand new list three two, one,
- 10:12Speaker 2: and it leaves your original list totally untouched, which makes sense, right,
- 10:16Speaker 2: But Python it returns absolutely nothing, literally a none value.
- 10:20Speaker 2: Wait really yeah, But silently in the background, it reaches
- 10:24Speaker 2: into memory and alters your original list.
- 10:27Speaker 1: That's horrifying.
- 10:28Speaker 2: And then JavaScript does both. It returns a reverse list
- 10:32Speaker 2: and it silently mutates the original list.
- 10:34Speaker 1: That is three totally different architectural behaviors for the exact
- 10:38Speaker 1: same command exactly. If I'm writing flight control software, I
- 10:42Speaker 1: cannot have my code silently changing the original flight coordinates
- 10:46Speaker 1: in the background while I'm just trying to calculate a
- 10:48Speaker 1: new trajectory.
- 10:49Speaker 2: And that is the danger of side effects. When a
- 10:52Speaker 2: function relies on or alters data outside of its own
- 10:56Speaker 2: inputs and outputs, you completely lose the ability to predict.
- 10:59Speaker 1: Your So by enforcing referential transparency, Haskell basically guarantees that
- 11:05Speaker 1: a function will never silently alter a hidden the state.
- 11:07Speaker 2: Exactly, and to accommodate this strictness, Haskell leans really heavily
- 11:11Speaker 2: on the wear clause.
- 11:12Speaker 1: Right right. So instead of you know, declaring a variable
- 11:15Speaker 1: at the top of a file, mutating at fifty times
- 11:17Speaker 1: and just hoping you know what it equals by line one.
- 11:19Speaker 2: Hundred, you write your logic first.
- 11:22Speaker 1: Yeah, you just write the equation change given ode, and
- 11:25Speaker 1: then underneath that you define the reality of that equation
- 11:29Speaker 1: where given is ten and ode is five.
- 11:31Speaker 2: It reverses the normal order to prioritize the readability of
- 11:34Speaker 2: the mathematical logic. You state the truth and then you
- 11:37Speaker 2: define the components of that truth.
- 11:39Speaker 1: It's beautiful, But practically speaking, if variables can't change and
- 11:43Speaker 1: functions must be mathematically pure, doesn't your code base just
- 11:47Speaker 1: become this bloated dictionary of highly specific named mathematical operations.
- 11:52Speaker 2: It could if you didn't have lambdas.
- 11:55Speaker 1: Ah, Because if I need to do some really quick
- 11:57Speaker 1: on the fly calculation, I don't want a formally declare
- 12:01Speaker 1: and name a dedicated function for it.
- 12:03Speaker 2: You know, and you don't have to, Which brings us
- 12:05Speaker 2: to the actual implementation of Alonzo Church's theories lambda functions.
- 12:09Speaker 1: These are the nameless functions.
- 12:11Speaker 2: Exactly anonymous functions. And the syntax for this in Haskell
- 12:14Speaker 2: is really clever. It uses a backslash because visually a
- 12:18Speaker 2: backslash looks a bit like the Greek lambda symbol, So.
- 12:21Speaker 1: You just write backslash x arrow x like x.
- 12:25Speaker 2: Right. It's just a tiny function that takes an argument
- 12:27Speaker 2: and immediately returns it.
- 12:29Speaker 1: And they are completely ephemeral totally.
- 12:32Speaker 2: You use them for ad hoc computations. You can write
- 12:34Speaker 2: a tiny self contained piece of logic like x x
- 12:37Speaker 2: two and immediately feed at the number four on the
- 12:40Speaker 2: exact same line.
- 12:42Speaker 1: It evaluates to eight, and then the function just disappears.
- 12:44Speaker 2: Poof gone. But their real power, and this is crucial,
- 12:49Speaker 2: is how they allow you to construct lexical scope entirely
- 12:53Speaker 2: from scratch.
- 12:54Speaker 1: Okay, scope scope being the context in which a variable actually.
- 12:58Speaker 2: Exists precisely, Like if you create a definition inside a function,
- 13:03Speaker 2: does the rest of the program know about it? Can
- 13:05Speaker 2: it accidentally interfere with something else?
- 13:06Speaker 1: It's like a pop up tent?
- 13:08Speaker 2: Oh? I like that?
- 13:09Speaker 1: Yeah, a Lamba function operates like a pop up tent.
- 13:11Speaker 1: You deployed exactly when and where you need a temporary,
- 13:14Speaker 1: self contained environment. The zipper on that tent completely seals
- 13:18Speaker 1: off the outside world. Right, You do your calculation inside
- 13:21Speaker 1: and as soon as you step out, the tent vanishes.
- 13:23Speaker 1: It leaves no permanent footprint on the campsite.
- 13:26Speaker 2: That boundary is so critical because lacking that boundary causes
- 13:31Speaker 2: massive real world damage. Kurt actually points out a notorious
- 13:35Speaker 2: historical flaw in JavaScript regarding.
- 13:37Speaker 1: This, Oh, the global variable disaster.
- 13:40Speaker 2: Yes, early JavaScript lacked proper module boundaries. Yeah, if you
- 13:44Speaker 2: wrote a quick function and casually typed C equals A
- 13:47Speaker 2: plus B without explicitly defining C as a strictly local variable.
- 13:51Speaker 1: JavaScript would panic assume you meant it globally and just
- 13:55Speaker 1: attach C to the global environment.
- 13:57Speaker 2: Right, Meaning, any other script on the entire website that
- 14:00Speaker 2: happened to use the letter C, for say, a loop or.
- 14:02Speaker 1: A counter was instantly overwritten.
- 14:04Speaker 2: Overwritten, a tiny harmless addition function could literally crash a
- 14:08Speaker 2: multimillion dollar e commerce checkout page.
- 14:10Speaker 1: That is wild, So how did they fix it?
- 14:12Speaker 2: Well? To stop the bleeding, JavaScript developers adopted a pattern
- 14:16Speaker 2: called the IIE, the immediately invoked function expression.
- 14:19Speaker 1: Which is basically a lambda exactly.
- 14:22Speaker 2: They would wrap their potentially dangerous code inside an anonymous
- 14:25Speaker 2: lambda function and execute it immediately it creates your pop
- 14:28Speaker 2: up tet.
- 14:29Speaker 1: So the variable c is trapped inside the lambda scope,
- 14:32Speaker 1: it calculates, returns the answer, and vanishes before it can
- 14:36Speaker 1: poison the global environment.
- 14:37Speaker 2: Yep, so these nameless functions aren't just academic theory, you know,
- 14:42Speaker 2: they are literally the architectural duct tape that kept the
- 14:45Speaker 2: early web from collapsing.
- 14:46Speaker 1: That is so cool. And in Haskell that concept is
- 14:49Speaker 1: refined into let expressions. Right.
- 14:51Speaker 2: Yes, a let expression combines the weed ability of a
- 14:55Speaker 2: wear clause with the inline scoped power of a lambda.
- 15:00Speaker 1: It just allows you to create safe, temporary realities for
- 15:03Speaker 1: your logic to execute within completely effortlessly. But wait, if
- 15:07Speaker 1: lamb does prove that we can create these nameless functions
- 15:10Speaker 1: on the fly, just floating in our code, it forces
- 15:13Speaker 1: a pretty wild realization, which is we can treat these
- 15:16Speaker 1: functions exactly like any other piece of raw data.
- 15:19Speaker 2: Ah. Yes, the paradigm of first class functions. Whereas class functions,
- 15:23Speaker 2: it means that a function in your language is a
- 15:25Speaker 2: citizen with the exact same rights as an integer or
- 15:28Speaker 2: a string of texts.
- 15:29Speaker 1: Meaning you can pass a function as an argument into
- 15:31Speaker 1: another function.
- 15:32Speaker 2: Yes, and passing a function into a function is usually
- 15:36Speaker 2: where traditional programmers hit a massive.
- 15:38Speaker 1: Wall, it breaks your brain a little bit. At first,
- 15:40Speaker 1: Let's look at Will Kurtz if even example. Okay, say
- 15:44Speaker 1: you have a program that needs to manipulate numbers, but
- 15:47Speaker 1: only if they are even. In a normal language, you
- 15:50Speaker 1: might write a function called if even to add one.
- 15:53Speaker 1: Then later you realize you need to double even numbers,
- 15:56Speaker 1: so you write if even double. Then you need to
- 15:59Speaker 1: square them, so you write if even square.
- 16:01Speaker 2: You just end up writing identical if the and logic
- 16:04Speaker 2: blocks over and over again. The only thing changing is
- 16:07Speaker 2: the specific math operation at the very end.
- 16:09Speaker 1: But with first class functions you don't do that. You
- 16:12Speaker 1: just write one generic function called if even, and.
- 16:15Speaker 2: It takes two arguments, a number and another function.
- 16:18Speaker 1: If the number is even, it applies whatever function you
- 16:21Speaker 1: just handle it. So you just pass it the ink
- 16:23Speaker 1: function or the double function.
- 16:25Speaker 2: But the crucial question here is how the program handles
- 16:28Speaker 2: this without accidentally executing the function too early.
- 16:31Speaker 1: Oh right, Because in normal math, operations have a specific order,
- 16:35Speaker 1: but here functions are always evaluated before operators.
- 16:37Speaker 2: Yes, function precedence is absolute. That's why if you type
- 16:41Speaker 2: ink two to three in Haskell, it evaluates to nine,
- 16:44Speaker 2: not seven, It increments the two first, turning it into three.
- 16:48Speaker 2: That multiplies by three.
- 16:49Speaker 1: Which saves the programmer from drowning and endless parentheses.
- 16:53Speaker 2: Exactly. So, when you pass double into if even, you
- 16:56Speaker 2: aren't passing the result of doubling a number. You are
- 16:59Speaker 2: passing a reference to the logic itself.
- 17:02Speaker 1: Because Haskell utilizes lazy evaluation, right, yes, it creates.
- 17:06Speaker 2: What's called a thunk. Thunk a delayed computation. A thunk,
- 17:11Speaker 2: it holds the function and its arguments in memory as
- 17:14Speaker 2: a concept, and it only physically evaluates the math that
- 17:17Speaker 2: the absolute last microsecond when the final result is actually
- 17:20Speaker 2: demanded by the system.
- 17:21Speaker 1: It's like handing someone a recipe card instead of a
- 17:23Speaker 1: baked cake.
- 17:24Speaker 2: That's a perfect way to put it.
- 17:25Speaker 1: They just hold the instructions until they actually need to eat,
- 17:28Speaker 1: which creates incredibly modular code.
- 17:30Speaker 2: Oh insanely modular.
- 17:33Speaker 1: Kirk gives this great example with custom sorting. Imagine sorting
- 17:36Speaker 1: a list of tupoles that contain first and last names. Okay,
- 17:40Speaker 1: Haskell has a built in sort by function that handles
- 17:43Speaker 1: all the heavy lifting of moving the items around in memory.
- 17:46Speaker 1: All you have to do is write a tiny custom
- 17:48Speaker 1: rule a function called compare last names, and you just
- 17:51Speaker 1: pass that rule into sort bay.
- 17:53Speaker 2: The overarching architecture the sorting algorithm is completely decoupled from
- 17:58Speaker 2: the specific business logic of how you want to handle,
- 18:01Speaker 2: say a tiebreaker between two identical last names.
- 18:04Speaker 1: It's so elegant. Okay, So passing functions as arguments is powerful.
- 18:09Speaker 1: But if functions are just data, that means a function
- 18:12Speaker 1: can evaluate some logic and return a completely different function
- 18:15Speaker 1: as its output.
- 18:16Speaker 2: Yes, and this is how functional programming handles complex routing
- 18:20Speaker 2: without building massive, fragile webs of ithin statements.
- 18:24Speaker 1: The book illustrates this with a really fun scenario involving
- 18:26Speaker 1: a secret society of contemporary alchemists.
- 18:28Speaker 2: Oh I love this part. They are sending out newsletters
- 18:30Speaker 2: right right, and.
- 18:31Speaker 1: They have regional post offices in San Francisco, New York,
- 18:34Speaker 1: and Reno, and each office has wildly different, highly specific
- 18:40Speaker 1: formatting rules for how the mailing address needs to look
- 18:43Speaker 1: on the envelope to maintain their secrecy.
- 18:45Speaker 2: Right, And in traditional von Neumann style program you would
- 18:48Speaker 2: build one massive function with a giant web of nested
- 18:52Speaker 2: conditionals to parse the city and format the.
- 18:55Speaker 1: String, which becomes a total nightmare to maintain, oh absolutely,
- 18:58Speaker 1: But treating functions as completely changes the architecture. You write
- 19:03Speaker 1: a separate, mathematically pure function for each city, any office,
- 19:07Speaker 1: cess office, reno office.
- 19:09Speaker 2: They do one thing perfectly exactly.
- 19:11Speaker 1: Then you write a dispatch function called get location function.
- 19:15Speaker 2: And the dispatcher takes a simple string, right like any
- 19:18Speaker 2: SF or RENO. It evaluates that string using a case expression.
- 19:22Speaker 1: But it doesn't return a formatted address.
- 19:24Speaker 2: No, it returns the actual function required for that city.
- 19:26Speaker 1: It hands you the specific tool you need for the job.
- 19:29Speaker 2: Exactly, and it even handles failure gracefully using a wildcard.
- 19:32Speaker 1: Oh right, because in Haskell, the underscore is a catch all.
- 19:36Speaker 2: Yeah, so if someone inputs, say law for Los Angeles,
- 19:41Speaker 2: the case expression just hits the wild card and returns
- 19:45Speaker 2: a generic anonymous lamb to function that just prints the
- 19:48Speaker 2: name normally.
- 19:49Speaker 1: For you listening. Just think about the architectural freedom this provides.
- 19:53Speaker 1: You completely decouple the decision making process from the execution process.
- 19:57Speaker 2: It's huge, Yeah, because if the Secret Society opens a
- 20:01Speaker 2: new branch in Chicago, you don't tear open a massive
- 20:03Speaker 2: web of logic to insert.
- 20:05Speaker 1: A new if statement, right, You just write a tiny
- 20:07Speaker 1: Chicago office function add one single line to the dispatcher
- 20:10Speaker 1: pointing to it, and you're done.
- 20:12Speaker 2: The system is infinitely expandable and perfectly safe.
- 20:15Speaker 1: Because there's no hidden state for the new Chicago function
- 20:18Speaker 1: to accidentally mutate.
- 20:19Speaker 2: Exactly.
- 20:19Speaker 1: We started this whole discussion comparing sequential directions to a map,
- 20:23Speaker 1: and by digging into get programming with Haskell, I think
- 20:26Speaker 1: we've seen exactly how abandoning sequential instructions for mathematical purity
- 20:31Speaker 1: completely changes the game.
- 20:32Speaker 2: We really did. We moved from manipulating memory slots to
- 20:35Speaker 2: evaluating universal.
- 20:37Speaker 1: Truth, pure functions, referential transparency, variables that act as definitions, lambdoscopes,
- 20:45Speaker 1: and treating logic as raw data.
- 20:48Speaker 2: That's a lot to take in.
- 20:49Speaker 1: It is, and these constraints they definitely feel restrictive when
- 20:52Speaker 1: you first encounter them, but they yield systems that are
- 20:55Speaker 1: just incredibly safe and modular. They do, and honestly, even
- 20:59Speaker 1: if you never write a single line of a functional language,
- 21:02Speaker 1: learning this paradigm changes how you approach abstraction in any field.
- 21:06Speaker 2: Well, the implications extend far beyond software. How so, Well,
- 21:10Speaker 2: we learned today that system bugs are almost always caused
- 21:13Speaker 2: by hidden states and side effects. Right, So, think about
- 21:17Speaker 2: the massive complex systems in your own life. Okay, your
- 21:20Speaker 2: daily workflows, your team's communication at your job, or even
- 21:23Speaker 2: just how you organize your physical space. Oh wow, how
- 21:26Speaker 2: many bugs in your daily life are caused by hidden
- 21:28Speaker 2: states and invisible side effects?
- 21:30Speaker 1: Probably most of them?
- 21:31Speaker 2: Right, So what would happen if you applied functional stateless
- 21:34Speaker 2: thinking to your daily routines?
- 21:36Speaker 1: That is man, That is a completely different map of reality.
- 21:40Speaker 1: It really is definitely something to think about. Thanks for
- 21:43Speaker 1: joining us on this deep dive and we'll see you
- 21:45Speaker 1: next time.