From 57db2e1f15c895ee67edf497d77af97e91657c6c Mon Sep 17 00:00:00 2001 From: Andrew Kuhnhausen Date: Sat, 30 Jul 2016 14:43:48 -0700 Subject: [PATCH 1/4] First draft of boolean This is incomplete, but I want to get this up early for review to make sure it is a useful level of abstraction and discussion There are several things we can talk about here: - Hoare logic - Decision trees Obviously, we need actual code here. Ref: #4 --- introduction/boolean.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 introduction/boolean.md diff --git a/introduction/boolean.md b/introduction/boolean.md new file mode 100644 index 0000000..df87cad --- /dev/null +++ b/introduction/boolean.md @@ -0,0 +1,21 @@ +## Boolean + +Boolean means True or False. Would it surprise you to find out that the fundamentals of computation are built entirely out of True and False? We often call this concept "binary" -- meaning two choices. You'll often here these two names, "binary" and "boolean" used to describe the same concept. + +One of the primary things a programmer does everyday is figuring out how to take a human concept and turn it into something that the computer can run. Being such a fundamental building block of programming means that really complex processes can be broken down into chunks of what we can call "boolean logic". + +Our REPL uses boolean logic. + +The Read phase is boolean: "has the user hit Enter/Return yet"? If the answer is yes/true, then it takes whatever text was input before and sends that input to the Evaluate phase. + +The Evaluation phase is boolean: the outcome of the evaluation can do two things: success or failure. This is a binary choice. It will send either a successful value or an error to the Print phase. + +The Print phase is boolean: either it prints an error message or the value that was passed to it from the evaluation phase. + +The Loop phase is also boolean: if the evaluation was successful it will remember the evaluated input so that it can be used during the next Read phase, otherwise it will forget what was input. + +Let's try a thinking exercise... + +How does sending a tweet work using boolean logic? + +How can you describe opening a door, drinking coffee, driving a car in terms of boolean logic? From 03bb211db78ce201497b155669262477fd6f8c70 Mon Sep 17 00:00:00 2001 From: Andrew Kuhnhausen Date: Sat, 30 Jul 2016 15:49:31 -0700 Subject: [PATCH 2/4] Add concrete examples of boolean operations This adds some playground type stuff for the REPL and sticks it at the beginning of this section, leaving the larger discussion for after students have time to play around with the REPL. Ref: #4 --- introduction/boolean.md | 75 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/introduction/boolean.md b/introduction/boolean.md index df87cad..33a02d9 100644 --- a/introduction/boolean.md +++ b/introduction/boolean.md @@ -2,17 +2,82 @@ Boolean means True or False. Would it surprise you to find out that the fundamentals of computation are built entirely out of True and False? We often call this concept "binary" -- meaning two choices. You'll often here these two names, "binary" and "boolean" used to describe the same concept. +In Scala, the `Boolean` type has two concrete values: `true` and `false`. + +``` +scala> true +res1: Boolean = true + +scala> false +res2: Boolean = false +``` + +There are several operations that can be run with Booleans, but let's focus on two: + +- `&&` pronounced: "and" +- `||` pronounced: "or" + +Try typing a series of `true` and `false` values separated by those operations, e.g. `true && true || false`. + +What do you expect the following values to be for the following operations: + +``` +scala> false || false || false || true +res0: Boolean = ??? + +scala> true && true && false +res1: Boolean = ??? +``` + +Anything that eventually becomes a Boolean can use these `&&` and `or` operations. + +Let's take our example from the [Variables](variables.md) section and play around with Booleans. + +``` +scala> val apples = 2 +apples: Int = 2 + +scala> val oranges = 2 +oranges: Int = 3 + +scala> val thereAreMoreApplesThanOranges = apples > oranges +thereAreMoreApplesThanOranges: Boolean = false +``` + +Notice the `>` (greater-than) operation? This makes intuitive sense that two numbers can be compared this way. We also have other operations on numbers that end up with a Boolean value. + +- `>` greater-than +- `<` less-than +- `==` equal +- `!=` not-equal + +Notice that `thereAreMoreApplesThanOranges` is a `Boolean`? That means it is either `true` or `false` (in this case, it's `false`) and we can use it like any other `Boolean`. + +``` +scala> (oranges > apples) && (apples != oranges) +res3: Boolean = true + +scala> val moreOrangesThanApples = oranges > apples +moreOrangesThanApples: Boolean = true + +scala> val numOfApplesIsNotSameAsNumOfOranges = apples != oranges +numOfApplesIsNotSameAsNumOfOranges: Boolean = true + +scala> moreOrangesThanApples && numOfApplesIsNotSameAsNumOfOranges +res4: Boolean = true +``` + One of the primary things a programmer does everyday is figuring out how to take a human concept and turn it into something that the computer can run. Being such a fundamental building block of programming means that really complex processes can be broken down into chunks of what we can call "boolean logic". -Our REPL uses boolean logic. +Guess what? Our REPL uses boolean logic! -The Read phase is boolean: "has the user hit Enter/Return yet"? If the answer is yes/true, then it takes whatever text was input before and sends that input to the Evaluate phase. +The *Read* phase is boolean: "has the user hit Enter/Return yet"? If the answer is yes/true, then it takes whatever text was input before and sends that input to the *Eval* phase. -The Evaluation phase is boolean: the outcome of the evaluation can do two things: success or failure. This is a binary choice. It will send either a successful value or an error to the Print phase. +The *Eval* phase is boolean: the outcome of the evaluation can do two things: success or failure. This is a binary choice. It will send either a successful value or an error to the *Print* phase. -The Print phase is boolean: either it prints an error message or the value that was passed to it from the evaluation phase. +The *Print* phase is boolean: either it prints an error message or the value that was passed to it from the evaluation phase. -The Loop phase is also boolean: if the evaluation was successful it will remember the evaluated input so that it can be used during the next Read phase, otherwise it will forget what was input. +The *Loop* phase is also boolean: if the evaluation was successful it will remember the evaluated input so that it can be used during the next Read phase, otherwise it will forget what was input. Let's try a thinking exercise... From 7df1ac5c50441e28341d6f7af259e0fec3125dcf Mon Sep 17 00:00:00 2001 From: Andrew Kuhnhausen Date: Sat, 30 Jul 2016 15:51:43 -0700 Subject: [PATCH 3/4] Fix typo in boolean --- introduction/boolean.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/introduction/boolean.md b/introduction/boolean.md index 33a02d9..2abd996 100644 --- a/introduction/boolean.md +++ b/introduction/boolean.md @@ -1,6 +1,6 @@ ## Boolean -Boolean means True or False. Would it surprise you to find out that the fundamentals of computation are built entirely out of True and False? We often call this concept "binary" -- meaning two choices. You'll often here these two names, "binary" and "boolean" used to describe the same concept. +Boolean means True or False. Would it surprise you to find out that the fundamentals of computation are built entirely out of True and False? We often call this concept "binary" -- meaning two choices. You'll often hear these two names, "binary" and "boolean" used to describe the same concept. In Scala, the `Boolean` type has two concrete values: `true` and `false`. From 1b2f0e19aedafb3d3402c3dcc4866f95f8e592de Mon Sep 17 00:00:00 2001 From: Andrew Kuhnhausen Date: Sat, 30 Jul 2016 16:54:32 -0700 Subject: [PATCH 4/4] Add some context and more examples Boolean logic is so amazing that I can barely controll my excitement. I hope to pursuade students to explore the things they learn by playing in the REPL and trying to answer their own questions by trying it out. Ref: #4 --- introduction/boolean.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/introduction/boolean.md b/introduction/boolean.md index 2abd996..3713f5d 100644 --- a/introduction/boolean.md +++ b/introduction/boolean.md @@ -2,6 +2,8 @@ Boolean means True or False. Would it surprise you to find out that the fundamentals of computation are built entirely out of True and False? We often call this concept "binary" -- meaning two choices. You'll often hear these two names, "binary" and "boolean" used to describe the same concept. +This is only the beginning of the rabbit hole of what is called "Boolean Logic". One of the most exciting parts of programming is that the things you use and learn have deep roots in rich concepts that apply to so much more than you might imagine. The fun part is that we have this REPL that we get to play with and explore these things. So let's play! + In Scala, the `Boolean` type has two concrete values: `true` and `false`. ``` @@ -17,9 +19,11 @@ There are several operations that can be run with Booleans, but let's focus on t - `&&` pronounced: "and" - `||` pronounced: "or" -Try typing a series of `true` and `false` values separated by those operations, e.g. `true && true || false`. +Try typing a series of `true` and `false` values separated by those operations, e.g. `true && true || false`. What do you think these operations are doing? + +One of the fun things about your REPL is you can play around with things that you may not already understand! -What do you expect the following values to be for the following operations: +Given what you think those operations do, what do you expect the values to be for the following: ``` scala> false || false || false || true @@ -27,6 +31,9 @@ res0: Boolean = ??? scala> true && true && false res1: Boolean = ??? + +scala> true || false +res2: Boolean = ??? ``` Anything that eventually becomes a Boolean can use these `&&` and `or` operations.