1 00:00:06,080 --> 00:00:09,552 If you've ever played 20 Questions, you know that the answer to true/false 2 00:00:09,552 --> 00:00:13,694 questions can really say a lot. And in fact, in computer programs, 3 00:00:13,694 --> 00:00:16,760 true/false questions turn out to be fundamental. 4 00:00:18,240 --> 00:00:21,485 In this video, what I want to do is talk about a new kind of value called the 5 00:00:21,485 --> 00:00:26,190 Boolean that's going to represent the answer to true/false questions. 6 00:00:27,290 --> 00:00:31,125 I'm also going to talk about a number of primitive true/false questions and a new 7 00:00:31,125 --> 00:00:34,901 expression in Racket called if that's going to let us make our programs produce 8 00:00:34,901 --> 00:00:40,410 different values depending on the answer to true/false questions. 9 00:00:41,510 --> 00:00:45,450 Boolean values are going to represent the answer to true false questions. 10 00:00:45,450 --> 00:00:49,418 So you shouldn't be surprised to find that there's two Boolean values, and in 11 00:00:49,418 --> 00:00:53,448 the beginning student language, we write them this way: true if the value that 12 00:00:53,448 --> 00:00:56,982 represents the true answer to a true/false question and false is the 13 00:00:56,982 --> 00:01:01,012 value that represents a false answer and since their values if we run them if we 14 00:01:01,012 --> 00:01:07,260 evaluate them we get the value themselves. 15 00:01:07,260 --> 00:01:11,859 Okay, let me comment those out. How we're going to represent the answer 16 00:01:11,859 --> 00:01:15,753 one of the questions. Now, let me just set myself up here a 17 00:01:15,753 --> 00:01:21,300 bit, I'll give myself two constants, width and height. 18 00:01:21,300 --> 00:01:25,305 I'll just make them both be a 100. Okay? 19 00:01:25,305 --> 00:01:31,820 So here is a true/false question. Here's a primitive called greater than. 20 00:01:31,820 --> 00:01:37,049 And if we say greater than width, height, and evaluate that, run that, we get 21 00:01:37,049 --> 00:01:41,585 false. Because width is 100, height is 100, and 22 00:01:41,585 --> 00:01:47,837 100 isn't greater than 100. On the other hand, here's another 23 00:01:47,837 --> 00:01:52,329 primitive. Greater than or equal width, height, and 24 00:01:52,329 --> 00:01:57,103 if we run that now, we get false for the first one and true for the second one, 25 00:01:57,103 --> 00:02:02,123 because 100 is greater than or equal to 100. 26 00:02:04,190 --> 00:02:07,718 These kinds of primitives that produce a true/false value are often called 27 00:02:07,718 --> 00:02:11,470 predicates, so we would say that greater than is a predicate, and greater than or 28 00:02:11,470 --> 00:02:17,844 equal to is a predicate. And, there's lot of others. 29 00:02:17,844 --> 00:02:25,730 You could say equals 1 2 or equals 1 1. Let me just comment these two outs so 30 00:02:25,730 --> 00:02:28,910 they don't confuse us. Or, greater than 3 9. 31 00:02:28,910 --> 00:02:34,203 And we'll run that. And equals 1 2 is giving us false. 32 00:02:34,203 --> 00:02:41,468 Equals 1 1 is giving us true. And, or, rather greater, than 3 9 is 33 00:02:41,468 --> 00:02:46,325 giving us false. And there's lots of others and I'll 34 00:02:46,325 --> 00:02:51,529 remind you that the 1x lecture video will show you how to discover more. 35 00:02:53,780 --> 00:02:59,342 Let me just show you a couple more. I'll comment this out now so it doesn't 36 00:02:59,342 --> 00:03:05,190 confuse us. There's some that operate on strings. 37 00:03:05,190 --> 00:03:13,430 So, for example, you could say string equals question mark foo bar. 38 00:03:13,430 --> 00:03:16,920 And that's false, because those two strings are not equal. 39 00:03:16,920 --> 00:03:21,617 What if I were to ask questions about images, so let's see, suppose I want to 40 00:03:21,617 --> 00:03:29,427 have two images like this. I1 is a rectangle that's 10 by 20 and 41 00:03:29,427 --> 00:03:38,275 it's solid and it's red and I2 is a rectangle and it's 20 by 10 and it's 42 00:03:38,275 --> 00:03:51,178 solid and it's blue. Suppose I want to know which image is 43 00:03:51,178 --> 00:04:08,120 thinner, I could say less than image with I1 image width I2, and I could run that. 44 00:04:09,750 --> 00:04:13,530 Oh, let's see, rectangle, this function is not defined oh, this is the usual 45 00:04:13,530 --> 00:04:18,660 mistake, wanted to use image functions and I forgot to put a required. 46 00:04:18,660 --> 00:04:26,427 I'll just go put require 2htdp/image. And now, if I run this, I'm getting true, 47 00:04:26,427 --> 00:04:34,760 meaning that the width of I1 which is 10 is less than the width of I2 which is 20. 48 00:04:34,760 --> 00:04:38,070 Okay, so that's Boolean values true and false. 49 00:04:38,070 --> 00:04:41,910 And, a number of predicates that are questions that produce true/false 50 00:04:41,910 --> 00:04:45,644 answers. Now I want to talk about if expression, 51 00:04:45,644 --> 00:04:48,668 which are going to let us have our program produce different results, 52 00:04:48,668 --> 00:04:51,962 depending on the answer to true false questions, depending on the result 53 00:04:51,962 --> 00:04:57,460 produced by a predicate. If expressions are simple to form. 54 00:04:57,460 --> 00:05:04,980 It's open paren, if and then three expressions, and a closed paren. 55 00:05:04,980 --> 00:05:08,067 We call those three expressions the question, the true answer and the false 56 00:05:08,067 --> 00:05:11,003 answer. And one key thing is that the question 57 00:05:11,003 --> 00:05:15,287 expression has to produce the boolean value, so the question expression usually 58 00:05:15,287 --> 00:05:20,734 calls a predicate of some form. Now, let's look at an example. 59 00:05:20,734 --> 00:05:24,628 Going back to where we were, let me start by just deleting this expression here, 60 00:05:24,628 --> 00:05:28,345 and then, we'll try to write an if expression that determines what the shape 61 00:05:28,345 --> 00:05:34,025 is of I1. So we'll say open paren if, and then, 62 00:05:34,025 --> 00:05:41,980 let's say, less than is the width of I1 less than height of I1. 63 00:05:41,980 --> 00:05:52,690 And if that's true, that's the question. If the question is true, then here is the 64 00:05:52,690 --> 00:05:58,077 true answer. We're going to say that the image is tall 65 00:05:58,077 --> 00:06:00,991 if its width is less than its height and in the tall sense, we'll say that the 66 00:06:00,991 --> 00:06:05,816 image is wide. And if I run that now, there's two 67 00:06:05,816 --> 00:06:12,271 interesting things to see here. One is that the result was tall, because 68 00:06:12,271 --> 00:06:16,626 sure enough I1 is taller than it is wide, and the other interesting thing to see 69 00:06:16,626 --> 00:06:22,720 was that right here, this piece of code was highlighted in black. 70 00:06:22,720 --> 00:06:25,970 Depending on which version of Racket you're using, it might be highlighted in 71 00:06:25,970 --> 00:06:29,390 orange, but it's highlighted. We're going to talk a lot more about that 72 00:06:29,390 --> 00:06:32,711 later this week. But if I go ahead now and switch around, 73 00:06:32,711 --> 00:06:37,529 maybe I ask the question about I2 instead of I1, well, I2's width is greater than 74 00:06:37,529 --> 00:06:42,686 its height. And in this case, if we evaluate the 75 00:06:42,686 --> 00:06:46,526 expression, it produces wide and the other, the true answer this time is 76 00:06:46,526 --> 00:06:50,439 highlighted in black. So, you may be able to start to guess 77 00:06:50,439 --> 00:06:53,900 what that black highlighting means, but again, we'll talk about that later. 78 00:06:53,900 --> 00:06:58,780 So, that's an example of an if expression. 79 00:06:58,780 --> 00:07:01,972 What we need to do now is look at the detailed rules for evaluating if 80 00:07:01,972 --> 00:07:04,874 expressions. What I'm going to do is first go ahead 81 00:07:04,874 --> 00:07:07,754 and put the evaluation rules for if here on the right, and then, I'm going to 82 00:07:07,754 --> 00:07:10,874 change the example expression a little bit, just to make it a little bit richer 83 00:07:10,874 --> 00:07:15,290 so we'll understand the rules of it better. 84 00:07:15,290 --> 00:07:21,000 I'll say that if the width is less than the height will produce the width. 85 00:07:21,000 --> 00:07:26,500 That, the true answer will be the width and the false answer will be the height. 86 00:07:26,500 --> 00:07:30,080 Now, let's try to step through that bit by bit. 87 00:07:30,080 --> 00:07:33,564 And I'll also use the Ctrl+E or Command+E command to just make it rank the high the 88 00:07:33,564 --> 00:07:36,619 [UNKNOWN]. So I've got a bit more room to work. 89 00:07:36,619 --> 00:07:40,206 So let's see, I've got an if expression. And the rule for evaluating an if 90 00:07:40,206 --> 00:07:44,812 expression is the first thing I have to do is evaluate the question expression. 91 00:07:44,812 --> 00:07:49,036 So there's the question expression, the question expression is a call to the 92 00:07:49,036 --> 00:07:53,115 primitive less than. So the first thing I have to do is reduce 93 00:07:53,115 --> 00:07:57,553 all of its operands to values. Let's see, the first operand isn't a 94 00:07:57,553 --> 00:08:01,394 value, it's an expression. In particular, it's a call to the 95 00:08:01,394 --> 00:08:04,810 primitive image width, and the first thing I have to do is reduce all of its 96 00:08:04,810 --> 00:08:09,298 operands to values. G, this first operand isn't a value, it's 97 00:08:09,298 --> 00:08:13,630 the name of a constant. So I have to produce a value for red. 98 00:08:13,630 --> 00:08:20,945 So, the first evaluation step [SOUND] simply is going to reduce that 99 00:08:20,945 --> 00:08:30,160 expression, the constant name, to the corresponding value. 100 00:08:30,160 --> 00:08:36,095 In this case, what that does is that I2, it comes the actual image, image value. 101 00:08:36,095 --> 00:08:43,644 Okay, so now let's see. If expression reduced the question, 102 00:08:43,644 --> 00:08:52,167 reduced the operands to this last stand. Here's the first operand that's imaged 103 00:08:52,167 --> 00:08:56,700 with that uh-huh. What happens here is that this whole 104 00:08:56,700 --> 00:09:00,130 thing, I'm just copying it and pasting it. 105 00:09:00,130 --> 00:09:05,360 This whole thing, we just end up reducing this expression here this time. 106 00:09:05,360 --> 00:09:11,920 So that's going to become 20, the width of rectangle 2. 107 00:09:11,920 --> 00:09:14,020 The next step, well, at this point, I think you could see how this is going to 108 00:09:14,020 --> 00:09:16,270 work. Now, we're basically reducing this 109 00:09:16,270 --> 00:09:20,692 operand to the last then. In the next step, the I2 becomes an 110 00:09:20,692 --> 00:09:26,128 image. And then, in the step after that, that 111 00:09:26,128 --> 00:09:33,764 whole thing becomes 10. And I'll just put it on one line like 112 00:09:33,764 --> 00:09:40,515 this so we get a bit more room to work with. 113 00:09:40,515 --> 00:09:44,483 So remember, what we did here is we had to evaluate the if expression, if wants 114 00:09:44,483 --> 00:09:48,220 to evaluate its question expression first. 115 00:09:48,220 --> 00:09:52,022 This was a call to a primitive, in which both oeprands as calls to primitive. 116 00:09:52,022 --> 00:09:55,011 So there's a fair amount of evaluation work we have to do here before we've 117 00:09:55,011 --> 00:09:58,343 evaluated the question expression, and we still aren't even done, we still have a 118 00:09:58,343 --> 00:10:04,965 call to a primitive. Both operands are values, so now, we can 119 00:10:04,965 --> 00:10:11,493 actually call less than with the two values. 120 00:10:11,493 --> 00:10:20,500 Now, less than, now less than 20 10, well, 20 is not less than 10. 121 00:10:20,500 --> 00:10:25,996 So that's clearly going to be false. And now, finally, we've evaluated the 122 00:10:25,996 --> 00:10:29,641 question expression. We evaluated the question expression, and 123 00:10:29,641 --> 00:10:32,964 now, we go to the remaining rules of the if. 124 00:10:32,964 --> 00:10:37,620 Next rule say's that if the question expression produces true. 125 00:10:37,620 --> 00:10:40,339 Well, it hasn't. Here, the question expression has 126 00:10:40,339 --> 00:10:43,494 produced false. So the next rule say's if the question 127 00:10:43,494 --> 00:10:47,613 expression produces false. Then what we're going to do is replace 128 00:10:47,613 --> 00:10:52,772 the entire if expression with the false answer, so there is the false answer, the 129 00:10:52,772 --> 00:10:58,600 entire expression gets replaced with a false answer. 130 00:10:58,600 --> 00:11:03,340 Now we have a call to a primitive, first operand is not a value so we need to 131 00:11:03,340 --> 00:11:14,310 evaluate it. So that has to get evaluated. 132 00:11:14,310 --> 00:11:19,539 Now, all the operand statement type are values so we can actually call image 133 00:11:19,539 --> 00:11:26,718 height and we get 10, and that's going, the whole thing evaluates to 10. 134 00:11:26,718 --> 00:11:29,994 And again, if we use that trick I showed you in the last video of running this, 135 00:11:29,994 --> 00:11:33,010 now, we'll see a whole bunch of 10s, which tells us that we did all of our 136 00:11:33,010 --> 00:11:38,425 evaluation steps properly. So the key thing is the rule for if is 137 00:11:38,425 --> 00:11:43,675 first you evaluate the question, and then depending on the results of the question, 138 00:11:43,675 --> 00:11:48,700 you replace the entire if with either the true answer or the false answer and then 139 00:11:48,700 --> 00:11:55,018 you just keep evaluating. What I want to do now is back up just a 140 00:11:55,018 --> 00:11:59,262 little bit. Let's go back to when we just had image 1 141 00:11:59,262 --> 00:12:04,466 and image 2. And we know that if we want to find out 142 00:12:04,466 --> 00:12:12,403 whether image 1 has a height greater than image 2, we could say this. 143 00:12:12,403 --> 00:12:18,773 So that's comparing the height of I1 and the height of I2, and if we run that, 144 00:12:18,773 --> 00:12:24,920 we'll get true because 20 is bigger than 10. 145 00:12:24,920 --> 00:12:32,980 We can also, if we wanted to compare the width of I1 and the width of I2 to see, 146 00:12:32,980 --> 00:12:41,430 in some sense, this first thing is saying is I1 taller than I2 and the second one 147 00:12:41,430 --> 00:12:52,970 is saying is I1 skinnier than I2? And that's true. 148 00:12:52,970 --> 00:12:57,680 Now, what if we wanted to know whether I1 was both taller and skinnier than I2? 149 00:12:57,680 --> 00:13:03,748 If it was both things together. Well, here's a new primitive called and, 150 00:13:03,748 --> 00:13:08,520 and is a special kind of expression like if. 151 00:13:08,520 --> 00:13:12,600 And the way and works is you can wrap open paren around any number of other 152 00:13:12,600 --> 00:13:18,110 expressions that themselves are going to produce a Boolean value. 153 00:13:18,110 --> 00:13:23,213 And what and does is it evaluates the first one, and if that one produces true 154 00:13:23,213 --> 00:13:27,670 it keeps going. And if by the time it gets to the end all 155 00:13:27,670 --> 00:13:32,716 of the expressions have produced true, then the whole and produces true. 156 00:13:32,716 --> 00:13:37,352 If on the other hand and gets to an expression that produces false, then it 157 00:13:37,352 --> 00:13:43,227 stops right away and produces false. So and does what's called short 158 00:13:43,227 --> 00:13:46,444 circuiting. It doesn't keep evaluating if it comes to 159 00:13:46,444 --> 00:13:50,291 an expression that produces false. It only keeps going as long as the 160 00:13:50,291 --> 00:13:54,952 expressions are producing true. There's also a primitive or expression 161 00:13:54,952 --> 00:13:58,918 and there's a primitive function called not. 162 00:13:58,918 --> 00:14:02,948 You can look both of those up in the help desk and you can watch this Week's I1 163 00:14:02,948 --> 00:14:07,010 video to find out how to use the help desk.