Latest / CyberSecurity Summary / Introduction to Programming and Problem-Solving Using Scala (Chapman & Hall / CRC Textbooks in Computing)
Transcript
- 0:00Speaker 1: Imagine you're writing a piece of financial software for I
- 0:03Speaker 1: don't know, a major bank or something.
- 0:05Speaker 2: Oh yeah, no pressure at all.
- 0:06Speaker 1: Right, So you're in charge of calculating daily interest, and
- 0:09Speaker 1: you are positive your math is completely flawless. You run
- 0:14Speaker 1: a really simple test calculation, like just to be sure,
- 0:17Speaker 1: one point zero minus zero point nine minus zero point one.
- 0:20Speaker 2: Which obviously a child could tell you equals zero exactly.
- 0:25Speaker 1: But when you actually run the code, the computer spits
- 0:28Speaker 1: out negative two point seven, which is I mean, that's
- 0:32Speaker 1: just wild, it is, And you're sitting there wondering why.
- 0:35Speaker 1: And the crazy thing is is because the device you're
- 0:37Speaker 1: listening to this on right now, beneath all that sleek
- 0:40Speaker 1: glass and the intuitive menus, it doesn't actually understand human math.
- 0:44Speaker 2: It really doesn't. We interact with these interfaces and we
- 0:47Speaker 2: just you know, we assume there's this digital magic happening
- 0:49Speaker 2: behind the screen. Yeah, but if you peel that back,
- 0:51Speaker 2: you uncover this landscape of just pure, rigid mathematical instruction
- 0:56Speaker 2: that is totally alien to how the human brain actually works.
- 1:00Speaker 1: Which forces us to confront this pretty huge truth. Right right,
- 1:03Speaker 1: we are trusting our lives, our economies, our private data
- 1:07Speaker 1: to machines that are essentially, at their core, just incredibly fast,
- 1:12Speaker 1: very complicated light.
- 1:13Speaker 2: Switches, just billions of tiny switches flipping on and off.
- 1:17Speaker 1: Welcome to the Deep Dive. I'm your host, and today
- 1:20Speaker 1: we're getting into something really special. Whether you're an aspiring
- 1:23Speaker 1: coder looking to build the next big app, or maybe
- 1:26Speaker 1: you just want to automate a few boring office tasks.
- 1:29Speaker 2: Or honestly, if you're just insanely curious about how this
- 1:32Speaker 2: whole digital world operates. I'm your resident tech expert, and
- 1:35Speaker 2: I am so ready to get into this.
- 1:36Speaker 1: Glad to have you so for you listening, Today's Deep
- 1:39Speaker 1: Dive is for you. We are extracting the hidden logic
- 1:42Speaker 1: behind the screens you stare at all day. And our
- 1:45Speaker 1: main source for this is this remarkably detailed textbook called
- 1:48Speaker 1: Introduction to Programming and Problem Solving using Skala. It's the
- 1:52Speaker 1: second edition by Marcy Lewis and Lisa L.
- 1:55Speaker 2: Latcher, and I have to say this text is totally
- 1:58Speaker 2: unique because it refuses to to just hand you a
- 2:01Speaker 2: cheat sheet of code syntax and say good luck.
- 2:03Speaker 1: Right. It doesn't just say type this to make that
- 2:05Speaker 1: happen exactly.
- 2:07Speaker 2: The authors really insist on building your understanding from the
- 2:10Speaker 2: literal physical machinery up. They trace the evolution of computational
- 2:15Speaker 2: thought all the way up to high level abstract logic.
- 2:18Speaker 1: Okay, let's unpack this because our mission today isn't just
- 2:21Speaker 1: to memorize a programming language. I mean we want to
- 2:24Speaker 1: understand the fundamtal nature of problem solving itself.
- 2:27Speaker 2: Right, yeah, how we basically teach fundamentally stupid machines to
- 2:33Speaker 2: do incredibly smart things.
- 2:35Speaker 1: And what surprised me is that the text starts this
- 2:37Speaker 1: journey centuries before electricity was even a thing we could harness.
- 2:41Speaker 2: Oh yeah, it goes way back. The authors actually quote
- 2:43Speaker 2: edsgar Dykstra.
- 2:45Speaker 1: Oh I love this quote.
- 2:46Speaker 2: It's so good. He famously said computer science is no
- 2:50Speaker 2: more about computers than astronomy is about telescopes.
- 2:53Speaker 1: Which is just a brilliant way to frame it.
- 2:54Speaker 2: It really is. The physical machine is just the tool.
- 2:57Speaker 2: The actual science we're talking about is the study of
- 2:59Speaker 2: algorith and an algorithm is you know, it's just a
- 3:02Speaker 2: formal specification for solving a problem.
- 3:05Speaker 1: Right, And the text mentions that the concept is named
- 3:07Speaker 1: after an eleventh century mathematician, al Quarizmi. So people were
- 3:12Speaker 1: theorizing about algorithmic logic long long before anyone even imagined
- 3:17Speaker 1: a silicon chip, centuries before I noticed. The text also
- 3:20Speaker 1: highlights Charles Babbage. He's designing the analytical engine in the
- 3:23Speaker 1: eighteen thirties, and he's literally building this massive mechanical beast
- 3:28Speaker 1: out of brass years and levers.
- 3:30Speaker 2: Yeah, a steam powered computer basically.
- 3:33Speaker 1: But the real breakthrough, at least conceptually, comes from Ada Lovelace.
- 3:38Speaker 1: She looks at Babbage's giant calculator and realizes something profound.
- 3:42Speaker 2: She saw the bigger picture, she really did.
- 3:44Speaker 1: She sees that if a machine can manipulate numbers according
- 3:47Speaker 1: to rules, and those numbers can represent other things like say,
- 3:51Speaker 1: musical notes or letters, then the machine can manipulate any
- 3:54Speaker 1: logical symbol, which is.
- 3:55Speaker 2: Just fascinating because Lovelace basically decoupled computation from mere arithmetic.
- 4:00Speaker 2: She envisioned a machine that could process pure logic.
- 4:03Speaker 1: But that theoretical foundation wouldn't really get solidified until what
- 4:07Speaker 1: the nineteen thirties.
- 4:08Speaker 2: Right the nineteen thirties, that's when you have mathematicians like
- 4:11Speaker 2: Kurt Godele with his incompleteness theorem. He was proving that
- 4:15Speaker 2: formal mathematical systems had inherent limits.
- 4:18Speaker 1: Like there are things math just can't prove.
- 4:20Speaker 2: About itself exactly, and that prompted Alan Turing and Alonso
- 4:24Speaker 2: Church to independently start developing mathematical models to define what
- 4:29Speaker 2: computation actually is, like what can and cannot be solved
- 4:33Speaker 2: by just following a set of rules.
- 4:34Speaker 1: The Turing machine is the model that always blows my
- 4:37Speaker 1: mind because when you read the actual mechanics of it
- 4:39Speaker 1: in the text, it sounds so aggressively simple.
- 4:43Speaker 2: It really does. And it's important to remember it wasn't
- 4:44Speaker 2: a real machine. It was just a thought experiment, right.
- 4:47Speaker 1: So he imagined this infinitely long strip of paper tape
- 4:50Speaker 1: divided into little squares, and then you have a mechanical
- 4:53Speaker 1: head that can do only four basic things. It can
- 4:56Speaker 1: read the symbol on the current square, write a new symbol,
- 4:59Speaker 1: move one square left, or move one square right. That's it.
- 5:02Speaker 1: That's it, and the only thing guiding it is a
- 5:04Speaker 1: basic table of rules, which they call a state table.
- 5:08Speaker 2: And around the same time Alonzo Church developed a parallel
- 5:11Speaker 2: concept using pure mathematics called the lambda calculus.
- 5:15Speaker 1: Okay, so a totally different approach.
- 5:17Speaker 2: Totally different. It approached the exact same problem, not with
- 5:20Speaker 2: the tape in a head, but by treating computation as
- 5:23Speaker 2: the evaluation of mathematical functions. But together they basically proved
- 5:28Speaker 2: that these incredibly basic systems could compute absolutely anything that
- 5:33Speaker 2: is theoretically computable. Wow, and that's what we call the
- 5:36Speaker 2: Church touring thesis.
- 5:37Speaker 1: Okay, but I've got to play Devil's advocate for the
- 5:39Speaker 1: listener here. If I'm listening to this and I just
- 5:41Speaker 1: want to write a quick script to like sort some
- 5:43Speaker 1: customer data from my job, why do I need to
- 5:46Speaker 1: care about a hypothetical paper tape machine from nineteen thirty
- 5:49Speaker 1: six or Church's abstract math.
- 5:52Speaker 2: I get that, But understanding the Turing machine is what
- 5:55Speaker 2: demystifies the physical computer sitting on your desk right now.
- 5:58Speaker 2: When the text introduces the von Neumann architecture, which the
- 6:01Speaker 2: blueprint for basically all modern computers, it is literally just
- 6:06Speaker 2: Turing's thought experiment brought into the physical world.
- 6:09Speaker 1: Wait really, how so? Well?
- 6:10Speaker 2: Think about it. The central processing unit the CPU that
- 6:15Speaker 2: is Turing's read slash right head. The random access memory
- 6:20Speaker 2: or RAM that's the infinite paper tape. Oh yeah, and
- 6:24Speaker 2: they're connected by a physical bus, which is literally just
- 6:27Speaker 2: a series of microscopic copper wires ferrying electrical signals back
- 6:31Speaker 2: and forth between the head and the tape.
- 6:33Speaker 1: So the modern processor is just reading an instruction from RAM,
- 6:37Speaker 1: executing it, writing a result, and moving to the next one,
- 6:40Speaker 1: exactly like the tape machine.
- 6:41Speaker 2: Exactly. It just does it billions of times a second.
- 6:44Speaker 1: That makes so much sense, And the text mentions modern advancements,
- 6:48Speaker 1: you know, like multi core processors, which, if I'm understanding
- 6:51Speaker 1: this right, means we've basically just jammed multiple read write
- 6:54Speaker 1: heads onto the tame chip to read different parts of
- 6:57Speaker 1: the tape at the same time, like multi threading.
- 6:59Speaker 2: Yep, you nailed it. Or take virtualization right, where we
- 7:02Speaker 2: run software that mimics a physical computer inside an actual
- 7:06Speaker 2: physical server.
- 7:07Speaker 1: So one physical machine acts like a dozen.
- 7:09Speaker 2: Right, simply to save electricity in space. But underneath all
- 7:12Speaker 2: those modern complex layers, the basic action is identical. It
- 7:16Speaker 2: loads the state, executes an instruction, and moves on.
- 7:20Speaker 1: So what does this all mean for us as programmers?
- 7:22Speaker 1: We have this physical von Neumann machine operating on literal
- 7:25Speaker 1: electoral pulses. How do we bridge the gap between a
- 7:29Speaker 1: physical transistor flipping on and off and a human thought
- 7:32Speaker 1: like calculate my hourly wage.
- 7:34Speaker 2: Well, that gap is crossed through a pretty painful evolution
- 7:38Speaker 2: of language, because at the lowest level, the machine only
- 7:40Speaker 2: understands binary base two, just ones and zeros, which.
- 7:44Speaker 1: Just represent the presence or absence of electrical voltage, right.
- 7:46Speaker 2: Exactly, high voltage is a one, low voltage is a zero.
- 7:49Speaker 2: The book provides this wild example of using memsi's machine
- 7:53Speaker 2: language just to calculate a simple wage like rate multiplied
- 7:56Speaker 2: by hours.
- 7:56Speaker 1: I saw that, and it is a complete nightmare for
- 7:59Speaker 1: a hue and to look at just to multiply two
- 8:02Speaker 1: tiny numbers. You're feeding the machine these massive thirty two
- 8:05Speaker 1: bit strings of binary. It's things like one zero zero
- 8:08Speaker 1: zero one one phase zero zero zero zero zero, and
- 8:11Speaker 1: on and on.
- 8:12Speaker 2: And if you prefer hexadecimal, it's just a block of
- 8:15Speaker 2: characters like eight c zero two zero zero zero zero.
- 8:19Speaker 1: Yeah. And if you make a typo and miss a
- 8:21Speaker 1: single zero, oh, the.
- 8:22Speaker 2: Entire program crashes and you have absolutely no idea.
- 8:25Speaker 1: Why, because it's completely unreadable. Right.
- 8:27Speaker 2: Human brains simply cannot parse raw binary efficiently. So engineers
- 8:32Speaker 2: developed assembly language.
- 8:34Speaker 1: Okay, so that was the next step up exactly.
- 8:36Speaker 2: Instead of typing that long string of binary, you type
- 8:39Speaker 2: a short mnemonic like LW for loadword. You still have
- 8:44Speaker 2: to basically micromanage the CPU's memory, but a program called
- 8:47Speaker 2: an assembler translates your LW back into the raw binary
- 8:51Speaker 2: for the machine.
- 8:52Speaker 1: Got it.
- 8:52Speaker 2: But eventually we evolved again to high level languages like.
- 8:56Speaker 1: Skala, which is where we actually want to be.
- 8:58Speaker 2: Right, So now you just write way equals rate times hours,
- 9:02Speaker 2: and a compiler or interpreter does the heavy lifting. It
- 9:05Speaker 2: translates that abstract thought down through assembly all the way
- 9:08Speaker 2: into those millions of ones and zeros.
- 9:11Speaker 1: This raises a really important question though, about how those
- 9:13Speaker 1: ones and zeros actually represent the data we're talking about. Like,
- 9:17Speaker 1: positive numbers in binary make sense to me. It's just
- 9:20Speaker 1: counting in base two instead of base ten. But how
- 9:23Speaker 1: do you tell a machine to hold a negative number?
- 9:26Speaker 2: Ah, that's the trick, isn't it.
- 9:28Speaker 1: Yeah, because the hardware just has electrical switches. There's no
- 9:31Speaker 1: physical minus switch you can flip on a circuit board.
- 9:33Speaker 2: No, there isn't. So the solution to this is a
- 9:37Speaker 2: truly brilliant piece of engineering called two's complement.
- 9:41Speaker 1: Okay, I have to admit, the textbook explanation of two's
- 9:44Speaker 1: complement totally derailed my brain. At first.
- 9:46Speaker 2: It's notoriously tricky for beginners.
- 9:49Speaker 1: I just assumed engineers would reserve the very first bit,
- 9:53Speaker 1: like the first zero or one in the string, to
- 9:55Speaker 1: act as a plus or minus sign, but instead to
- 9:58Speaker 1: make a binary number negative, the computer flips every single bit.
- 10:02Speaker 1: Every zero becomes a one, every one becomes a zero,
- 10:05Speaker 1: and then and this is the part that got me,
- 10:07Speaker 1: it adds one to the final result. Right. That just
- 10:09Speaker 1: sounds incredibly convoluted.
- 10:11Speaker 2: It sounds like it, yeah, but let me break down
- 10:14Speaker 2: why it's actually an elegant shortcut for the hardware. Imagine
- 10:17Speaker 2: we have a tiny four bit computer. Okay, the number
- 10:21Speaker 2: positive one in binary is zero, zero, zero one. To
- 10:24Speaker 2: make it negative one, we apply two's complement. First, we
- 10:28Speaker 2: flip all the bits, which gives us one, one, one.
- 10:31Speaker 1: Zero, right, just the exact opposite.
- 10:32Speaker 2: Then we add one, making it one one.
- 10:35Speaker 1: Okay, I am following the math, but I'm still stuck
- 10:37Speaker 1: on why we go through those gymnastics.
- 10:40Speaker 2: Because of what happens when you add them together. Let's
- 10:42Speaker 2: say you take positive one, zero, zero, zero one and
- 10:44Speaker 2: add our new negative one, which is one one one one.
- 10:48Speaker 2: The binary addition of those two results in one zero
- 10:52Speaker 2: zero zero zero. Okay, but remember our tiny computer only
- 10:56Speaker 2: holds four bits. That fifth bit, that one on the
- 10:58Speaker 2: far left, it literally off the physical end of the
- 11:01Speaker 2: memory register. It is completely discarded, and what you are
- 11:04Speaker 2: left with is zero zero zero zero zero.
- 11:06Speaker 1: Oh wait, so by formatting the negative number that way,
- 11:09Speaker 1: the hardware doesn't even need to know how to subtract exactly.
- 11:13Speaker 2: The physical logic gates that were built for addition can
- 11:16Speaker 2: process subtraction automatically because the overflow just rolls off the
- 11:20Speaker 2: edge of the chip.
- 11:20Speaker 1: That is wild. So by adapting the software's mathematical design
- 11:25Speaker 1: to the physical limitations of the hardware, the engineers saved
- 11:29Speaker 1: what enormous amounts of space on the silicon.
- 11:32Speaker 2: Enormous amounts of space and a huge amount of complexity.
- 11:35Speaker 1: I mean, that is just genius. Okay, So we have
- 11:37Speaker 1: this high level chem translating human intent down into these
- 11:40Speaker 1: clever hardware tricks. But when we actually sit down to
- 11:44Speaker 1: write that high level code, how do we structure it right?
- 11:48Speaker 2: How do we organize our thoughts?
- 11:49Speaker 1: Yeah? The text breaks this down to different programming paradigms
- 11:53Speaker 1: which it describes as essentially distinct philosophies of how to
- 11:56Speaker 1: organize your logic.
- 11:57Speaker 2: Yeah, and the foundational philosophy here is imperative programming. This
- 12:01Speaker 2: directly mirrors the physical machine language and the Turing machine
- 12:05Speaker 2: tape we discussed.
- 12:06Speaker 1: Earlier, step by step instructions.
- 12:08Speaker 2: Right, you give the computer explicit step by step physical instructions.
- 12:12Speaker 2: Take this value, add five to it, now move it
- 12:15Speaker 2: to this memory slot. Because you are constantly updating the
- 12:18Speaker 2: data as the program runs, it relies heavily on what
- 12:21Speaker 2: we call mutable state.
- 12:22Speaker 1: It's constantly changing exactly. And contrasting that is functional programming, which,
- 12:27Speaker 1: according to the text, was born directly out of Alonzo
- 12:31Speaker 1: Church's lambda calculuts. You have the math side, and it's declarative.
- 12:34Speaker 1: You aren't giving step by step physical instructions. You're simply
- 12:37Speaker 1: defining mathematical relationships. You tell the computer what you want,
- 12:42Speaker 1: not how to do it. And purely functional languages have
- 12:46Speaker 1: no mutable state at all. Right, the data never changes.
- 12:49Speaker 2: Never. You feed data into a function and it outputs
- 12:52Speaker 2: a brand new piece of data without ever altering the original.
- 12:55Speaker 1: To visualize the difference, I love the analogy the authors use.
- 12:58Speaker 1: They say think of an imperative program as a helpful
- 13:01Speaker 1: but incredibly messy roommate.
- 13:03Speaker 2: It's the perfect way to explain it.
- 13:04Speaker 1: You ask them to find a specific book in your room.
- 13:07Speaker 1: They go in, move your papers around, leave a pillow
- 13:09Speaker 1: on the floor, and pull the book out. So they
- 13:12Speaker 1: accomplish the task, but they altered the state of your room.
- 13:16Speaker 1: In programming, those are called side effects.
- 13:18Speaker 2: Right, You definitely know they were there because your environment
- 13:20Speaker 2: is permanently changed. But a functional program is like having
- 13:24Speaker 2: an invisible roommate. You ask for the book and they
- 13:27Speaker 2: retrieve it while leaving absolutely zero trace.
- 13:30Speaker 1: Your papers are untouched, the pillow is on the.
- 13:32Speaker 2: Bed, your room state remains perfectly pristine.
- 13:35Speaker 1: I love that. And then that desire for organization leads
- 13:39Speaker 1: to the third major paradigm, which is object oriented programming ORP.
- 13:45Speaker 2: Right. This philosophy takes the data and the functions that
- 13:48Speaker 2: operate on that specific data and binds them together into
- 13:52Speaker 2: these discrete, self contained packages called objects.
- 13:55Speaker 1: Which brings us to the star of the textbook Scala
- 13:59Speaker 1: and the name actually for scalable language. What makes it
- 14:02Speaker 1: totally unique is that it doesn't force you into just
- 14:04Speaker 1: one of those paradigms it's.
- 14:06Speaker 2: A true hybrid exactly underneath its syntax, Scala is purely
- 14:11Speaker 2: object oriented. Everything is an object, but the language is
- 14:14Speaker 2: explicitly designed to let the programmer seamlessly blend functional and
- 14:18Speaker 2: imperative styles.
- 14:19Speaker 1: So using our roommate analogy, Scala basically gives you a
- 14:22Speaker 1: roommate who defaults to being a perfectly invisible, clean functional programmer.
- 14:28Speaker 1: But if there's an emergency and you desperately need them
- 14:31Speaker 1: to tear the room apart to brute force a solution imperatively,
- 14:34Speaker 1: they have the tools to do that too.
- 14:36Speaker 2: And that flexibility is exactly why Scala is such a
- 14:39Speaker 2: powerful teaching language for you to learn with. It gives
- 14:41Speaker 2: you the clean reliability of functional programming, which honestly eliminates
- 14:46Speaker 2: entire categories of bugs, but it retains the step by
- 14:49Speaker 2: step imperative control when you really need to optimize for
- 14:52Speaker 2: raw speed.
- 14:53Speaker 1: Okay, let's look at how Scala actually executes this hybrid
- 14:56Speaker 1: approach and reality like. Starting with how a beginner interacts
- 14:59Speaker 1: with it. Authors rely really heavily on something called the
- 15:01Speaker 1: air EPL, the redevaluate print loop.
- 15:04Speaker 2: The RPL is great. Instead of forcing you to write
- 15:07Speaker 2: a massive script compile the whole thing and run it
- 15:10Speaker 2: just to see if a tiny piece of logic works.
- 15:13Speaker 2: The RPL lets you type a single line of code,
- 15:15Speaker 2: hit enter, and it instantly evaluates it and prints the result.
- 15:19Speaker 1: So you get immediate feedback.
- 15:20Speaker 2: Immediate feedback, which is crucial for learning. But as soon
- 15:24Speaker 2: as you start typing into it, you are immediately confronted
- 15:27Speaker 2: with how rigid the computer is about data.
- 15:29Speaker 1: Yeah, I noticed the text makes a huge deal out
- 15:31Speaker 1: of this. Yeah, Like, I can't just type the number five.
- 15:34Speaker 1: The system wants to know its type. Is it an int,
- 15:37Speaker 1: which is a thirty two bit integer, or a double,
- 15:40Speaker 1: which is a sixty four bit decimal. It differentiates between
- 15:44Speaker 1: a boolean for true false logic, a char for a
- 15:46Speaker 1: single character like the letter A, and a string for
- 15:49Speaker 1: a whole sentence.
- 15:50Speaker 2: It demands to know.
- 15:51Speaker 1: But why why does the machine care so much about
- 15:54Speaker 1: the difference doing the math number five and the text
- 15:56Speaker 1: character five. To me, they look the same.
- 15:58Speaker 2: Well, it all comes back to the physical memory. The
- 16:00Speaker 2: machine has to allocate a very specific amount of microscopic
- 16:04Speaker 2: real estate based on the type. A thirty two bit
- 16:07Speaker 2: integer needs exactly thirty two tiny electrical switches. A sixty
- 16:12Speaker 2: four bit decimal requires double that space.
- 16:14Speaker 1: Okay, that makes sense physically.
- 16:16Speaker 2: And furthermore, the binary sequence zero zero zero zero zero
- 16:19Speaker 2: one zero one means the mathematical number five to the
- 16:23Speaker 2: integer circuitry, but to the text processing circuitry, that exact
- 16:28Speaker 2: same binary sequence might represent a completely unreadable character. So
- 16:32Speaker 2: the type tells the compiler which hardware rules to actually apply.
- 16:36Speaker 1: Oh and here's where it gets really interesting. You mentioned
- 16:39Speaker 1: earlier that Scala is purely object oriented. Under the hood,
- 16:43Speaker 1: the text claims that in Scala, every single one of
- 16:45Speaker 1: those types is an object. Even a basic number like
- 16:48Speaker 1: five is an object everything, which means basic math operators
- 16:51Speaker 1: like a plus sign don't actually exist in the language
- 16:54Speaker 1: the way we were taught in grade school.
- 16:55Speaker 2: No, they don't. The plus sign is not a universal
- 16:58Speaker 2: mathematical truth in scis Skala. It is a method, a
- 17:01Speaker 2: function attached to an object.
- 17:03Speaker 1: Right, So if I type five plus six in the rapl,
- 17:06Speaker 1: I'm not using a universal math operator. Under the hood,
- 17:09Speaker 1: Scala looks at the object five, finds a method inside
- 17:13Speaker 1: it called plus, and passes the object six into that method.
- 17:17Speaker 2: Yes, it's actually reading five dot plus parentheses six.
- 17:20Speaker 1: But scoleotists drops the dot and the parentheses to make
- 17:23Speaker 1: it look like normal math. It's literally just syntax sugar
- 17:27Speaker 1: to keep our human brains comfortable.
- 17:29Speaker 2: It's brilliant language design, honestly. However, that reliance on hardware
- 17:34Speaker 2: rules over human intuition leads to some pretty severe oddities,
- 17:39Speaker 2: oh definitely. Like if you type five divided by two
- 17:41Speaker 2: into the rapl assuming it will give you two point five,
- 17:44Speaker 2: SCOLLA will simply return too, wait just two, just two
- 17:47Speaker 2: Because of integer truncation, If you ask the machine to
- 17:50Speaker 2: divide two integers, it absolutely refuses to create a decimal.
- 17:54Speaker 2: It gives you the whole number and silently throws the
- 17:57Speaker 2: remainder completely.
- 17:58Speaker 1: Away, just deletes it. So if you actually want throw,
- 18:00Speaker 1: you have to use a totally different tool like the
- 18:02Speaker 1: module operator exactly.
- 18:04Speaker 2: And if you want two point five you have to
- 18:05Speaker 2: explicitly feed the machine doubles. You have to type five
- 18:08Speaker 2: point zero divided by two point zero.
- 18:10Speaker 1: But even doubles have a dark side, don't they, yah,
- 18:12Speaker 1: which I think brings us right back to the banking
- 18:15Speaker 1: software failure we talked about at the very beginning of
- 18:18Speaker 1: the deep dive.
- 18:18Speaker 2: Yes, the one point zero minus point nine minus zero
- 18:21Speaker 2: point one resulting in negative two point seven.
- 18:24Speaker 1: So why does that actually happen?
- 18:25Speaker 2: Because doubles are floating point numbers. They represent decimals using
- 18:30Speaker 2: base two fractions, I think halves, quarters, eight, sixteenths. It's
- 18:35Speaker 2: like trying to write the fraction one third in our
- 18:37Speaker 2: normal base ten system.
- 18:39Speaker 1: All right, you get point three to three to three
- 18:41Speaker 1: repeating forever. Right.
- 18:43Speaker 2: You can never perfectly represent it. Could you run out
- 18:45Speaker 2: of paper eventually exactly, and the computer runs out of
- 18:47Speaker 2: sixty four bit memory space. There are many Base ten
- 18:50Speaker 2: decimals like point one that binary fractions simply cannot represent perfectly.
- 18:55Speaker 2: So the hardware gets as close as it physically can,
- 18:57Speaker 2: and then it just rounds off the end.
- 19:00Speaker 1: So it's slightly imprecise from the start right.
- 19:02Speaker 2: And when you chain those slightly imperfect numbers together in
- 19:05Speaker 2: a long calculation, those a microscopic rounding errors compound. They
- 19:10Speaker 2: build up until they produce a visibly incorrect result like
- 19:13Speaker 2: negative two point seven. Wow, which leads to a cardinal
- 19:17Speaker 2: rule of software engineering. You never ever use floating point
- 19:20Speaker 2: numbers for financial.
- 19:21Speaker 1: Software because the bank would lose money oh fast.
- 19:25Speaker 2: If you are dealing with currency, you use integers and
- 19:28Speaker 2: you count in sense. If you don't, your system will
- 19:31Speaker 2: slowly bleed real money just due to binary rounding.
- 19:34Speaker 1: That is a terrifyingly practical piece of advice. Okay, so
- 19:37Speaker 1: we have our data types, our objects are math. But
- 19:40Speaker 1: to build an actual program we have to store these
- 19:43Speaker 1: objects in memory so we can recall them later. We
- 19:45Speaker 1: have to name them, right.
- 19:47Speaker 2: Skala gives you two primary keywords to declare a name
- 19:50Speaker 2: for your data val which is short for value, and var,
- 19:54Speaker 2: short for variable, and both require you to initialize them immediately.
- 19:58Speaker 2: You can't just create an empty name and leave it
- 20:00Speaker 2: hanging there.
- 20:00Speaker 1: And the difference between val and var is essentially the
- 20:03Speaker 1: philosophical core of Skala, isn't.
- 20:04Speaker 2: It It really is.
- 20:05Speaker 1: I want to build on the box analogy the authors
- 20:08Speaker 1: used to visualize this because it helped me so much.
- 20:10Speaker 1: They say, don't think of the variable as the number itself.
- 20:14Speaker 1: Think of the variable as a cardboard box with a
- 20:16Speaker 1: name written on the side and sharpie okay, picture inside
- 20:19Speaker 1: that box is a wire, and that wire points to
- 20:22Speaker 1: the actual object living over somewhere else in the computer's memory.
- 20:26Speaker 2: That visualization captures the concept of memory references perfectly.
- 20:30Speaker 1: So if you declare your box using var, it means
- 20:33Speaker 1: that box is mutable. Later in the program, you can
- 20:36Speaker 1: reach into the box, unplug the wire from the number five,
- 20:39Speaker 1: and plug it into the number ten. Right, it can vary,
- 20:42Speaker 1: but if you declore the box using valel, that wire
- 20:45Speaker 1: is permently super glued to the original object. It is
- 20:48Speaker 1: completely immutable. You can never change what it points to exactly,
- 20:52Speaker 1: and you can even apply this to complex data. Right.
- 20:54Speaker 1: The text shows how you can group a price and
- 20:56Speaker 1: a weight into something called a tupole using a technique
- 20:59Speaker 1: called pattern matching. You can tape val breathis price, comma
- 21:03Speaker 1: weight and parenthesis equals t and Scala neatly unpacks the tuple,
- 21:07Speaker 1: assigning permanent wires to both pieces of data simultaneously.
- 21:10Speaker 2: And if we connect this to the bigger picture, we
- 21:13Speaker 2: arrive at the absolute golden rule of Scala programming. Prefer
- 21:17Speaker 2: VAL over VAR whenever humanly possible.
- 21:20Speaker 1: See my instinct is a beginner is the exact opposite.
- 21:23Speaker 1: If VAR is more flexible, why wouldn't I just use
- 21:26Speaker 1: it for everything?
- 21:26Speaker 2: Because flexibility at the micro level creates total chaos at
- 21:30Speaker 2: the macro level. Let's return to our roommate analogy. Using
- 21:34Speaker 2: VAR is the messy imperative roommate. Your program state is
- 21:39Speaker 2: constantly changing. Now, in a five line script, tracking a
- 21:43Speaker 2: change in variable is easy. But imagine a banking system
- 21:46Speaker 2: with three million lines of code tracking a single piece
- 21:50Speaker 2: of data that gets modified fifty different times by fifty
- 21:53Speaker 2: different functions. It's a logistical nightmare.
- 21:55Speaker 1: Oh, you would never be able to track down where
- 21:57Speaker 1: a bug.
- 21:57Speaker 2: Originated, precisely by forcing yourself to use while you are
- 22:00Speaker 2: adopting the functional invisible roommate style. Things that are immutable,
- 22:04Speaker 2: Things that stay exactly the same from the moment they
- 22:06Speaker 2: are created until the program ends are fundamentally easier for
- 22:10Speaker 2: the human brain to reason about.
- 22:11Speaker 1: It just removes the guesswork.
- 22:13Speaker 2: It completely eliminates entire categories of software bugs before they
- 22:17Speaker 2: even happen.
- 22:18Speaker 1: I imagine it takes a lot of practice to stop
- 22:20Speaker 1: preaching for var but it forces you to be a
- 22:23Speaker 1: much better problem solver. And really, that's what this entire
- 22:26Speaker 1: text has been driving at. Programming isn't just about memorizing syntax,
- 22:30Speaker 1: or knowing where to play semi cullins, or learning the
- 22:32Speaker 1: quirky math of a language like Scala. No, not at all.
- 22:35Speaker 1: It is a profound exercise in top down problem solving.
- 22:39Speaker 1: You are taking an incredibly ambiguous, messy human desire and
- 22:43Speaker 1: you are rigorously distilling it down into unambiguous logic. That
- 22:47Speaker 1: a piece of silicon can execute billions of times a
- 22:50Speaker 1: second without failure.
- 22:51Speaker 2: You cannot hide behind vague assumptions when you are talking
- 22:54Speaker 2: to a CPU. It forces absolute clarity of thought.
- 22:57Speaker 1: It's incredible. But early in the tech the authors drop
- 23:01Speaker 1: this philosophical bombshell that I just haven't been able to
- 23:03Speaker 1: stop thinking about.
- 23:04Speaker 2: Oh, I know the one you mean.
- 23:05Speaker 1: They suggest that the absolute bounds of computation might actually
- 23:09Speaker 1: be the bounds of human thinking itself, tying.
- 23:11Speaker 2: All the way back to the limits discovered by Godol
- 23:13Speaker 2: and the Churchturing thesis we talked about exactly.
- 23:17Speaker 1: So I want to leave you the listener with a
- 23:19Speaker 1: final thought to mull over today. If the Church Churing
- 23:22Speaker 1: thesis mathematically dictates the absolute limits of what a computational
- 23:27Speaker 1: machine can ever solve, and if human brains are ultimately
- 23:31Speaker 1: just complex biological machines processing electrical signals across a network
- 23:35Speaker 1: of neurons, does that mean human creativity, our intuition, and
- 23:39Speaker 1: even our consciousness are bound by the exact same mathematical
- 23:42Speaker 1: limits as a piece of scholar code, or is there
- 23:45Speaker 1: something truly uncomputable inside us now that raises an important
- 23:49Speaker 1: question about the nature of intelligence itself, one that is
- 23:52Speaker 1: certainly worth the deep dive of its own in the future.
- 23:54Speaker 1: It really is. It's just something to think about the
- 23:56Speaker 1: next time you tap that glass screen and expect magic.
- 24:00Speaker 1: Hes joining us on this deep dive, Keep questioning the
- 24:02Speaker 1: digital world around you, and we'll catch you next time.