1 00:00:01,170 --> 00:00:03,520 hello everybody welcome back 2 00:00:03,520 --> 00:00:07,279 at the next episode up functional programming 101 3 00:00:07,279 --> 00:00:10,679 and today we're going to talk about 4 00:00:10,679 --> 00:00:14,980 interactive programs I'm but before we start 5 00:00:14,980 --> 00:00:19,619 I want to share a story that I originally heard from Simon Peyton Jones 6 00:00:19,619 --> 00:00:23,890 and I'm in bed Jones and everybody should know 7 00:00:23,890 --> 00:00:27,070 is one of the main drivers behind 8 00:00:27,070 --> 00:00:30,269 has car an also am 9 00:00:30,269 --> 00:00:33,880 one of the founders of the Glasgow Haskell Compiler that 10 00:00:33,880 --> 00:00:36,940 we have been using in this course an 11 00:00:36,940 --> 00:00:40,040 here's the story so in this no box 12 00:00:40,040 --> 00:00:45,210 there is a super powerful a quantum computer 13 00:00:45,210 --> 00:00:50,559 it's extremely powerful it can go to a break every encryption scheme 14 00:00:50,559 --> 00:00:56,770 no known to man it can are sold and the problem it kind of cures cancer 15 00:00:56,770 --> 00:01:00,649 everything the only problem is that 16 00:01:00,649 --> 00:01:05,170 it has no I L so we will never know 17 00:01:05,170 --> 00:01:08,400 whether this quantum computer works 18 00:01:08,400 --> 00:01:12,840 are not the moral of the story is dead 19 00:01:12,840 --> 00:01:16,210 if your program is not able to communicate 20 00:01:16,210 --> 00:01:19,509 with the outside world it might as well 21 00:01:19,509 --> 00:01:24,780 not exist and if you go back to the early days of has gone 22 00:01:24,780 --> 00:01:29,400 it was actually really hard to have a Haskell program 23 00:01:29,400 --> 00:01:33,369 communicate with the outside world where's if you use 24 00:01:33,369 --> 00:01:36,790 and the other programming language it's trivial 25 00:01:36,790 --> 00:01:41,090 the first go to be alright hello world rich Prince hello world 26 00:01:41,090 --> 00:01:44,439 on the standard output whereas in Haskell 27 00:01:44,439 --> 00:01:48,140 that hello world Walsh super complicated 28 00:01:48,140 --> 00:01:53,030 first we had stream i/o then we had continuation based I L and 29 00:01:53,030 --> 00:01:56,439 finally and that you're going to talk about today 30 00:01:56,439 --> 00:02:01,270 we have the I O market and with the i/o Mon at 31 00:02:01,270 --> 00:02:03,900 an 32 00:02:03,900 --> 00:02:07,820 as call has gotten its I O ports 33 00:02:07,820 --> 00:02:12,090 an on the box such that they can easily communicate 34 00:02:12,090 --> 00:02:17,140 an with the outside world alright so 35 00:02:17,140 --> 00:02:21,260 so far we have only reading programs that 36 00:02:21,260 --> 00:02:27,430 take some and food and return a string or are an integer our top all our list 37 00:02:27,430 --> 00:02:30,730 and we have printed that value 38 00:02:30,730 --> 00:02:33,990 an on the out to adopt the 39 00:02:33,990 --> 00:02:37,520 and rebel above the of the GATT interpreter 40 00:02:37,520 --> 00:02:41,130 but what we really want to do is we want to ride 41 00:02:41,130 --> 00:02:44,520 programs like most programs like every 42 00:02:44,520 --> 00:02:47,560 Java program like every see sharp program like 43 00:02:47,560 --> 00:02:51,060 any impaired program that takes 44 00:02:51,060 --> 00:02:55,550 inputs from the actual world does something with it 45 00:02:55,550 --> 00:02:58,840 and produces output which and 46 00:02:58,840 --> 00:03:02,410 in fact what we want is that it produces aren't food 47 00:03:02,410 --> 00:03:05,780 as it's getting new Enbridge 48 00:03:05,780 --> 00:03:09,920 alright so we want to produce our bridge as forgetting 49 00:03:09,920 --> 00:03:14,730 UN which so what this program has to do 50 00:03:14,730 --> 00:03:18,020 it has two interact with the keyboard 51 00:03:18,020 --> 00:03:21,590 and discreet so and if we 52 00:03:21,590 --> 00:03:24,709 look at our program as a function 53 00:03:24,709 --> 00:03:28,709 and some says now it becomes dependent on the keyboard 54 00:03:28,709 --> 00:03:32,290 and b-sides just producing not good 55 00:03:32,290 --> 00:03:35,580 it also will communicate with discrete 56 00:03:35,580 --> 00:03:39,430 the problem 57 00:03:39,430 --> 00:03:43,260 is dead haskell programs are 58 00:03:43,260 --> 00:03:47,660 your functions which means they cannot have side effect 59 00:03:47,660 --> 00:03:50,739 and maybe it's good to go I'll 60 00:03:50,739 --> 00:03:53,739 informally and talk about 61 00:03:53,739 --> 00:03:57,730 what a mathematical function as dramatic a magmatic 'em 62 00:03:57,730 --> 00:04:00,730 law that contractor 63 00:04:00,730 --> 00:04:04,160 a mathematical function is 64 00:04:04,160 --> 00:04:07,440 a function that when you give it this same 65 00:04:07,440 --> 00:04:11,090 argument well always give you the same result 66 00:04:11,090 --> 00:04:14,160 am for example vectorial 67 00:04:14,160 --> 00:04:16,190 when I gave it an 68 00:04:16,190 --> 00:04:20,000 it will return factorial an when I give it and again 69 00:04:20,000 --> 00:04:25,080 it will return factorial and again now look at a common function 70 00:04:25,080 --> 00:04:28,670 that we use in impaired programming for example 71 00:04:28,670 --> 00:04:32,870 redline redline takes no arguments are 72 00:04:32,870 --> 00:04:38,780 and a Mt Dew bottle and readers a string when I call redline again 73 00:04:38,780 --> 00:04:43,330 with the same arguments the same empty to bomb federal return different string 74 00:04:43,330 --> 00:04:46,900 sure redline is not a mathematical function 75 00:04:46,900 --> 00:04:50,040 and the reason is is because it has side effects 76 00:04:50,040 --> 00:04:53,150 so it will return strain 77 00:04:53,150 --> 00:04:56,810 but there's something there that is not apparent in the dock 78 00:04:56,810 --> 00:05:00,330 and that's what's the i/o loan at an 79 00:05:00,330 --> 00:05:04,380 souls the i/o monette gonna expresses 80 00:05:04,380 --> 00:05:07,430 in the type over function that this function 81 00:05:07,430 --> 00:05:11,440 has a side effect Asia interactive programs programs that 82 00:05:11,440 --> 00:05:15,000 indirect with the outside world have side effects 83 00:05:15,000 --> 00:05:18,870 and we need a way to incorporate side effects 84 00:05:18,870 --> 00:05:22,540 in Haskell programs an not 85 00:05:22,540 --> 00:05:26,060 I don't think that side effect are bad am 86 00:05:26,060 --> 00:05:31,030 actually they are and super useful because you know without side effects is 87 00:05:31,030 --> 00:05:32,560 really hard to ride these 88 00:05:32,560 --> 00:05:37,180 interactive programs an but the thing is that you have to make sure 89 00:05:37,180 --> 00:05:40,330 again it's a balance it's a question of taste 90 00:05:40,330 --> 00:05:45,520 how you used shot those side effects it's a little bit like salt or spices 91 00:05:45,520 --> 00:05:46,240 and food 92 00:05:46,240 --> 00:05:50,850 you can overdo it your food is too salty and it doesn't taste well 93 00:05:50,850 --> 00:05:54,400 but if you don't use any salt your program your car 94 00:05:54,400 --> 00:05:57,510 your program your food is also not tasty 95 00:05:57,510 --> 00:06:01,080 a ride George up to you as a developer 96 00:06:01,080 --> 00:06:04,500 to decide how much side effects to use 97 00:06:04,500 --> 00:06:07,890 an but it's never good answer to say 98 00:06:07,890 --> 00:06:10,910 up I'm never going to use side-effect 99 00:06:10,910 --> 00:06:15,200 orange also never good the other extreme to say 100 00:06:15,200 --> 00:06:18,600 I don't care about purity a it is 101 00:06:18,600 --> 00:06:21,880 that's exactly what shows monster if programming 102 00:06:21,880 --> 00:06:25,100 that you can decide where do you side effect 103 00:06:25,100 --> 00:06:28,350 and where to avoid them but if I hear you say 104 00:06:28,350 --> 00:06:29,310 you know 105 00:06:29,310 --> 00:06:33,240 we have to program without any side effects you have not 106 00:06:33,240 --> 00:06:37,900 understood the passions of discourse are gay 107 00:06:37,900 --> 00:06:43,620 so as I said mentioned the solution to the problem is to introduce a new type 108 00:06:43,620 --> 00:06:48,160 and we really don't care how this type is defined 109 00:06:48,160 --> 00:06:51,210 an for for now we'll just issue 110 00:06:51,210 --> 00:06:54,480 its own abstract type I love a 111 00:06:54,480 --> 00:06:58,950 and this type denotes and action 112 00:06:58,950 --> 00:07:02,620 that will perform some side effects and as a result 113 00:07:02,620 --> 00:07:06,140 will return a value of type A 114 00:07:06,140 --> 00:07:12,160 and 115 00:07:12,160 --> 00:07:15,550 maybe a good way to 116 00:07:15,550 --> 00:07:20,840 remembered the difference is that in Haskell so far we've only used 117 00:07:20,840 --> 00:07:21,730 expressions 118 00:07:21,730 --> 00:07:24,990 but we didn't mention statements writers and bird 119 00:07:24,990 --> 00:07:29,340 language there are expressions and there are statements are statements 120 00:07:29,340 --> 00:07:32,560 mostly communicate via side effects and 121 00:07:32,560 --> 00:07:37,060 a.m. in some sense by entered using the i/o Mon at 122 00:07:37,060 --> 00:07:40,710 we are also injured using statements in Haskell 123 00:07:40,710 --> 00:07:44,390 or another way to say that this reintroduce the notion of 124 00:07:44,390 --> 00:07:48,600 actions in Haskell a but these actions themselves 125 00:07:48,600 --> 00:07:53,350 are also expression so they can be compost as you get some concrete 126 00:07:53,350 --> 00:07:54,230 examples 127 00:07:54,230 --> 00:07:57,530 of the i/o monette years I O of jar 128 00:07:57,530 --> 00:08:02,590 adjusted typeof in action that will perform some side effects 129 00:08:02,590 --> 00:08:06,080 and then return a character and then 130 00:08:06,080 --> 00:08:09,150 the and most comparative 131 00:08:09,150 --> 00:08:12,950 of impaired if types that is I O of unit 132 00:08:12,950 --> 00:08:16,790 that is a function that only perform side 133 00:08:16,790 --> 00:08:20,050 the only interesting thing it does is performing side effects 134 00:08:20,050 --> 00:08:24,780 because it has no real result value its readers the empty tubal 135 00:08:24,780 --> 00:08:28,210 am and in a typical imperative language 136 00:08:28,210 --> 00:08:32,800 this would be the type void so if you have a function that returns void 137 00:08:32,800 --> 00:08:36,280 in nashville that becomes function that returns 138 00:08:36,280 --> 00:08:39,520 type I O unit K 139 00:08:39,520 --> 00:08:42,180 and void is not think are you 140 00:08:42,180 --> 00:08:45,290 this Mt Dew ball is nothing special 141 00:08:45,290 --> 00:08:51,320 it's just a two ball and has no components now 142 00:08:51,320 --> 00:08:55,790 that we have this type in again we really don't care how diol more night is 143 00:08:55,790 --> 00:08:56,540 defined 144 00:08:56,540 --> 00:08:59,770 as long as we have operations 145 00:08:59,770 --> 00:09:03,399 defined on them okay so this is like a little bit like 146 00:09:03,399 --> 00:09:06,940 in object-oriented programming we have a class 147 00:09:06,940 --> 00:09:10,899 we have met the John that class in in order to use 148 00:09:10,899 --> 00:09:14,730 a glass you don't need to know how that class is implemented 149 00:09:14,730 --> 00:09:18,700 just you now used the mat and that's the same thing here 150 00:09:18,700 --> 00:09:22,690 with the i/o mod and the first operation that we have 151 00:09:22,690 --> 00:09:26,490 on the eye on it here is get jar in 152 00:09:26,490 --> 00:09:29,820 a typical imperative language GetJar 153 00:09:29,820 --> 00:09:33,040 has died unit arrow 154 00:09:33,040 --> 00:09:36,080 I O of jock unit arrow charge sorry 155 00:09:36,080 --> 00:09:41,220 and as go because we're lazy we don't need the unit arrow 156 00:09:41,220 --> 00:09:44,510 and so instead GetJar is just 157 00:09:44,510 --> 00:09:48,010 something it has a side effect and then 158 00:09:48,010 --> 00:09:51,820 returns a character now if if this would be strict 159 00:09:51,820 --> 00:09:56,150 it would kinda you know rear a immediately return a character right 160 00:09:56,150 --> 00:09:56,550 there 161 00:09:56,550 --> 00:10:00,240 but since has caused lazy in our we don't have to 162 00:10:00,240 --> 00:10:04,529 have this explicit dunk that d first evaluation 163 00:10:04,529 --> 00:10:08,040 let another and dressy thing 164 00:10:08,040 --> 00:10:13,810 in typical imperative languages whenever you see a function that has to be unit 165 00:10:13,810 --> 00:10:17,370 arrow something it means that you're trying to simulate 166 00:10:17,370 --> 00:10:21,350 laziness here's another 167 00:10:21,350 --> 00:10:24,790 an example the function 168 00:10:24,790 --> 00:10:29,360 board jar which rides a character to standard output 169 00:10:29,360 --> 00:10:32,820 it takes a character and edge returns void 170 00:10:32,820 --> 00:10:36,490 it has side effects but it doesn't return and the 171 00:10:36,490 --> 00:10:40,040 usual value and then an 172 00:10:40,040 --> 00:10:44,100 we have just like for parsers we have the 173 00:10:44,100 --> 00:10:48,339 action that immediately returns without doing anything 174 00:10:48,339 --> 00:10:53,140 okay so it takes a value and immediately readers that value 175 00:10:53,140 --> 00:10:54,840 without performing 176 00:10:54,840 --> 00:10:59,100 any for dinner side effects and then 177 00:10:59,100 --> 00:11:04,470 real me using the same kind of sequencing that were used for barger 178 00:11:04,470 --> 00:11:08,330 and again in the exercises you well 179 00:11:08,330 --> 00:11:12,710 see how an did this do notation 180 00:11:12,710 --> 00:11:16,510 is translated in terms of the binds 181 00:11:16,510 --> 00:11:19,520 function and but right now 182 00:11:19,520 --> 00:11:23,150 well just used to do notation because it looks very much like 183 00:11:23,150 --> 00:11:26,260 traditional impaired it got so here's a function 184 00:11:26,260 --> 00:11:30,990 that reach a character region other character but 185 00:11:30,990 --> 00:11:35,210 yeah ignores the result and then region third character 186 00:11:35,210 --> 00:11:40,040 and breeders the barrel does too so if you remember departures from the last 187 00:11:40,040 --> 00:11:40,650 lecture 188 00:11:40,650 --> 00:11:44,380 we had more or less exactly the same example here 189 00:11:44,380 --> 00:11:48,350 except that we used the I'd impartial three times and here 190 00:11:48,350 --> 00:11:54,400 for reading a character from the Standard & Poor's weekend 191 00:11:54,400 --> 00:11:58,800 define a function using GetJar 192 00:11:58,800 --> 00:12:03,630 that reads a string from the Standard & boot as follows: every 193 00:12:03,630 --> 00:12:07,980 she reread the character we call that character axe 194 00:12:07,980 --> 00:12:12,430 am notice that GetJar has died I O of jar 195 00:12:12,430 --> 00:12:16,010 and then X here has type char 196 00:12:16,010 --> 00:12:20,440 so the arrow is very similar to the arrow in generators in list 197 00:12:20,440 --> 00:12:21,780 comprehension show there's an 198 00:12:21,780 --> 00:12:25,160 I O of tea on the right and then there's a 199 00:12:25,160 --> 00:12:28,420 the on the left of the arrow and that's where why 200 00:12:28,420 --> 00:12:31,610 we use the arrow symbol and not shady 201 00:12:31,610 --> 00:12:36,340 ecosystem a bigoted type on this side is different than the dye 202 00:12:36,340 --> 00:12:41,000 on that well if the character that we just read is it 203 00:12:41,000 --> 00:12:45,230 and newline then were done so we return 204 00:12:45,230 --> 00:12:48,990 the empty string otherwise we recursively 205 00:12:48,990 --> 00:12:53,730 read a.m. Hong to read the rest of this character 206 00:12:53,730 --> 00:12:58,120 and then we return the previous character that we're at 207 00:12:58,120 --> 00:13:02,900 concatenated on the string that we're eager 208 00:13:02,900 --> 00:13:06,070 so another gonna recursive an 209 00:13:06,070 --> 00:13:09,610 thing here where we an just 210 00:13:09,610 --> 00:13:14,210 goal this thing itself recursively until we had the new line 211 00:13:14,210 --> 00:13:20,230 and then re return all values here's an example 212 00:13:20,230 --> 00:13:24,010 that takes a string righted do this standard 213 00:13:24,010 --> 00:13:29,580 all if we have the empty string reacted we cannot do anything so we just 214 00:13:29,580 --> 00:13:35,040 return unit immediately if we have a string that starts with the character 215 00:13:35,040 --> 00:13:37,380 axed followed by a string access 216 00:13:37,380 --> 00:13:41,530 be printed their first character and then recursively 217 00:13:41,530 --> 00:13:45,210 we print the rest of the string on the screen an 218 00:13:45,210 --> 00:13:50,330 this guy here readers I O unit so the whole thing readers i IO 219 00:13:50,330 --> 00:13:55,440 unit and everything Fitch now 220 00:13:55,440 --> 00:13:58,680 weekend am the fine 221 00:13:58,680 --> 00:14:02,700 another function that simply combines put string it puts our 222 00:14:02,700 --> 00:14:06,170 to ride the new line behind it so we first print the string 223 00:14:06,170 --> 00:14:13,170 and then reprinted newline the beauty of all this 224 00:14:13,170 --> 00:14:17,450 is that we can mix and match impaired if code or I O 225 00:14:17,450 --> 00:14:21,690 caught red all the functions that we have defined before 226 00:14:21,690 --> 00:14:26,040 and particular functions over less biggest strings are just less 227 00:14:26,040 --> 00:14:29,280 so weekend comparatively read 228 00:14:29,280 --> 00:14:32,490 string from the standard input dan 229 00:14:32,490 --> 00:14:37,270 you wish regular list 230 00:14:37,270 --> 00:14:40,690 functions on that strength and then 231 00:14:40,690 --> 00:14:43,870 and comparatively 232 00:14:43,870 --> 00:14:47,290 windy I Oman at ride this to the standard I 233 00:14:47,290 --> 00:14:51,240 which are large walk turn as example we a 234 00:14:51,240 --> 00:14:55,070 Brent and their strength be reached this 235 00:14:55,070 --> 00:14:58,900 line from the & Boch and then we print 236 00:14:58,900 --> 00:15:02,510 that deadline has so many characters okay 237 00:15:02,510 --> 00:15:05,920 very simple but you see here you combine there's an 238 00:15:05,920 --> 00:15:09,600 island of purity in the sea of impurity 239 00:15:09,600 --> 00:15:14,199 and dishes a perfect perfect perfect way to program 240 00:15:14,199 --> 00:15:18,279 so you have little parts of pure code embed 241 00:15:18,279 --> 00:15:23,589 in a context of impure and when we 242 00:15:23,589 --> 00:15:27,639 I'm run this string length in are nghe I 243 00:15:27,639 --> 00:15:31,930 ever well and print and torturing me type string 244 00:15:31,930 --> 00:15:36,130 and it will tell us that this string has five characters 245 00:15:36,130 --> 00:15:39,699 K and note that this thing is only 246 00:15:39,699 --> 00:15:43,259 evaluated for the side effects it doesn't return 247 00:15:43,259 --> 00:15:46,279 an useful value and we see that 248 00:15:46,279 --> 00:15:51,800 because it has type I'll unit this is the end of Part one 249 00:15:51,800 --> 00:15:57,680 see you in a few minutes when we're continuing to talk about the eye on it 250 00:15:57,680 --> 00:15:57,980 thank you