1 00:00:00,680 --> 00:00:04,220 welcome back I am with just seeing 2 00:00:04,220 --> 00:00:08,330 am some simple examples of the i/o monette 3 00:00:08,330 --> 00:00:12,980 that allows haskell program to communicate with the outside world 4 00:00:12,980 --> 00:00:16,330 and that gonna Indian 5 00:00:16,330 --> 00:00:20,109 that we emphasized lost that we have 6 00:00:20,109 --> 00:00:23,230 a CEO impaired if God with 7 00:00:23,230 --> 00:00:26,820 Islands of pure caught and the 8 00:00:26,820 --> 00:00:30,289 an this part of this lecture 9 00:00:30,289 --> 00:00:33,620 we're going to show a slightly larger example 10 00:00:33,620 --> 00:00:37,670 that also and features at this pattern 11 00:00:37,670 --> 00:00:40,899 so we will have and some impaired if God's 12 00:00:40,899 --> 00:00:44,940 that glues the are the program that we're going to ride to pure program that 13 00:00:44,940 --> 00:00:46,719 we're going to write with the outside world 14 00:00:46,719 --> 00:00:50,260 but we're going to use all the nice functions 15 00:00:50,260 --> 00:00:54,340 your functions that redefines before over lists 16 00:00:54,340 --> 00:00:57,420 we're going to use them here and to 17 00:00:57,420 --> 00:01:02,320 right the core of our program and 18 00:01:02,320 --> 00:01:06,400 the example that we're going to do is a very simple 19 00:01:06,400 --> 00:01:10,110 I'm version of the game of hangman and 20 00:01:10,110 --> 00:01:13,760 if you have never played hangman and its 21 00:01:13,760 --> 00:01:17,540 our popular game with kids and 22 00:01:17,540 --> 00:01:21,170 and what you do is wrong person types is sick 23 00:01:21,170 --> 00:01:24,750 has a secret typed words and then the other person 24 00:01:24,750 --> 00:01:29,710 as to dry the to guess the word by entering a sequence of gash 25 00:01:29,710 --> 00:01:32,869 and for each guess yeah are you will 26 00:01:32,869 --> 00:01:36,439 see which let their shit an occur 27 00:01:36,439 --> 00:01:40,220 and and you know as you go on you have to guess they work to 28 00:01:40,220 --> 00:01:43,750 gets played is and quite a lot and it's it's kinda fun 29 00:01:43,750 --> 00:01:48,299 and you know that the hangman part is that you only have a finite number of 30 00:01:48,299 --> 00:01:53,270 of dries and I you draw a picture and and you know I'd some point you die 31 00:01:53,270 --> 00:01:54,180 because you 32 00:01:54,180 --> 00:01:58,689 ran out of durch the game and 33 00:01:58,689 --> 00:02:02,530 when the word is correct am in this case 34 00:02:02,530 --> 00:02:06,799 we don't draw pictures or tried to gonna make you lose 35 00:02:06,799 --> 00:02:10,010 now let's 36 00:02:10,010 --> 00:02:12,720 start writing this God and 37 00:02:12,720 --> 00:02:16,780 what we're going to do has recounted take a top-down approach 38 00:02:16,780 --> 00:02:22,420 an where as we go we will design the part of the game that we need 39 00:02:22,420 --> 00:02:25,830 but the first start with the interaction with the outside world so 40 00:02:25,830 --> 00:02:29,450 what we do is we Brent to the output is 41 00:02:29,450 --> 00:02:32,630 think of a word and then we 42 00:02:32,630 --> 00:02:36,010 secretly read that work 43 00:02:36,010 --> 00:02:39,420 and this function secretly get line 44 00:02:39,420 --> 00:02:43,070 well REITs a string but it will 45 00:02:43,070 --> 00:02:48,200 echoed that string wit underscores and then 46 00:02:48,200 --> 00:02:51,750 the computer will ask us to tried to guess the word 47 00:02:51,750 --> 00:02:55,820 and then how do we do that well we typed in the word 48 00:02:55,820 --> 00:02:59,170 and then we're going to guess the word and 49 00:02:59,170 --> 00:03:02,650 we'll see but that function guess is 50 00:03:02,650 --> 00:03:07,170 in the next few sites first thing I want to show 51 00:03:07,170 --> 00:03:10,280 is this and function here 52 00:03:10,280 --> 00:03:14,520 secretly get line which reach a line of text from the keyboard 53 00:03:14,520 --> 00:03:19,020 echoing each character as a dash as a as an underscore 54 00:03:19,020 --> 00:03:23,990 so what do we do well it's a function that reaches string from the standard 55 00:03:23,990 --> 00:03:28,450 input show it has type I O string and what do we do 56 00:03:28,450 --> 00:03:31,760 well we first get the character 57 00:03:31,760 --> 00:03:35,890 if that character is an a new line 58 00:03:35,890 --> 00:03:39,950 then we echo the new line 59 00:03:39,950 --> 00:03:43,860 and then we return the empty string otherwise 60 00:03:43,860 --> 00:03:47,800 re brent's an underscore 61 00:03:47,800 --> 00:03:51,440 me recursively secretly read 62 00:03:51,440 --> 00:03:55,209 the rest have the strength and then we return 63 00:03:55,209 --> 00:03:58,890 the string that we read our act so 64 00:03:58,890 --> 00:04:04,350 and you can type this code then and dry dock nghe I am what you will see 65 00:04:04,350 --> 00:04:09,120 is dead as you type a word it will show dashes and it will return 66 00:04:09,120 --> 00:04:12,420 the word that you typed and Wed Aug revealing 67 00:04:12,420 --> 00:04:19,420 what the word loss because it only shows you how many characters there 68 00:04:19,840 --> 00:04:23,169 the am action getchar here 69 00:04:23,169 --> 00:04:26,289 and reach a character 70 00:04:26,289 --> 00:04:30,000 with arts am echoing it to the screen show what we have to do 71 00:04:30,000 --> 00:04:33,629 is we have to turn the an echoing off 72 00:04:33,629 --> 00:04:38,039 then we I read a character and then return the Act going on 73 00:04:38,039 --> 00:04:42,090 and then we actually returned the character so here you see 74 00:04:42,090 --> 00:04:46,069 that yeah this is typical impaired if code 75 00:04:46,069 --> 00:04:49,810 where you ride one statement another statement another statement 76 00:04:49,810 --> 00:04:54,389 another statement and one thing to note is that while this looks like an 77 00:04:54,389 --> 00:04:55,370 assignment 78 00:04:55,370 --> 00:04:59,069 it is really not an assignment right this is just saying 79 00:04:59,069 --> 00:05:02,979 this is type I of jar you binded 80 00:05:02,979 --> 00:05:06,689 to a character so there are side effects here 81 00:05:06,689 --> 00:05:09,759 because we're interacting with the environment 82 00:05:09,759 --> 00:05:13,319 but we're not using immutable fair 83 00:05:13,319 --> 00:05:16,569 and haskell the ship marketable their most 84 00:05:16,569 --> 00:05:19,629 an but we won't discuss them 85 00:05:19,629 --> 00:05:24,129 in in this lecture and but for that you know you have to 86 00:05:24,129 --> 00:05:28,330 at this special Mon at or you can do it in the eye Oman as well but you have to 87 00:05:28,330 --> 00:05:32,440 allocate I'm specifically the mutable they're both 88 00:05:32,440 --> 00:05:36,210 and write and read from them and this 89 00:05:36,210 --> 00:05:39,219 here is just because this is a moan at 90 00:05:39,219 --> 00:05:42,949 it just takes value of this computation 91 00:05:42,949 --> 00:05:49,520 and binds it to see she is not immutable very much alright 92 00:05:49,520 --> 00:05:53,490 another the I O part let's now 93 00:05:53,490 --> 00:05:56,750 look at the function guess so guess is the 94 00:05:56,750 --> 00:06:00,120 yeah I did the game nope which request 95 00:06:00,120 --> 00:06:04,629 guess s and then checks whether the guesses are right and Danielle 96 00:06:04,629 --> 00:06:08,800 until the an user 97 00:06:08,800 --> 00:06:12,020 hard a player has found the right answer 98 00:06:12,020 --> 00:06:18,529 so at to get caught we guessed the words in this word what's the secret word that 99 00:06:18,529 --> 00:06:19,270 we're at 100 00:06:19,270 --> 00:06:23,310 so what we do is we and Brent and 101 00:06:23,310 --> 00:06:26,330 abroad on the screen me 102 00:06:26,330 --> 00:06:30,930 read the gas and then if that works that 103 00:06:30,930 --> 00:06:34,680 retype then what's the secret word we're done 104 00:06:34,680 --> 00:06:38,580 so we say lol congratulations you got it 105 00:06:38,580 --> 00:06:42,330 otherwise we 106 00:06:42,330 --> 00:06:45,460 computed difference between the secret word 107 00:06:45,460 --> 00:06:48,509 and what the user type then 108 00:06:48,509 --> 00:06:52,389 be printed at different on the standard output 109 00:06:52,389 --> 00:06:56,360 and weed and recursively go 110 00:06:56,360 --> 00:07:00,610 and guess the word and here you see 111 00:07:00,610 --> 00:07:05,550 am where there's a tail recursion so this thing here gets word 112 00:07:05,550 --> 00:07:10,349 goals gets word again and if you're a gun large the player then 113 00:07:10,349 --> 00:07:14,780 like me you will have to usual although turns 114 00:07:14,780 --> 00:07:18,780 to and do this and you don't want this to stackoverflow 115 00:07:18,780 --> 00:07:23,300 an 116 00:07:23,300 --> 00:07:26,440 we're now back 117 00:07:26,440 --> 00:07:31,150 into the world of pure functions were finally leftist see 118 00:07:31,150 --> 00:07:34,900 of impaired if God and even reached a beautiful island 119 00:07:34,900 --> 00:07:39,139 of purity and there we're going to be fine dysfunction 120 00:07:39,139 --> 00:07:44,250 death function that takes two strings two strings 121 00:07:44,250 --> 00:07:47,630 and readers string that 122 00:07:47,630 --> 00:07:50,810 describe their difference and 123 00:07:50,810 --> 00:07:53,810 to define that function we're going to use it list comprehension 124 00:07:53,810 --> 00:07:57,289 right so what we say 125 00:07:57,289 --> 00:08:01,710 we take the two strings amber going to check 126 00:08:01,710 --> 00:08:05,759 for each character in the first drink for each 127 00:08:05,759 --> 00:08:09,020 axe in the first drink reeche 128 00:08:09,020 --> 00:08:12,409 if dat character 129 00:08:12,409 --> 00:08:16,380 appears in the second strain then 130 00:08:16,380 --> 00:08:20,020 we just returned that character so every character 131 00:08:20,020 --> 00:08:24,930 dead appears from the first string that appears in the second string we're just 132 00:08:24,930 --> 00:08:26,080 going to return 133 00:08:26,080 --> 00:08:30,990 and all characters from the first ring that don't appear in the second string 134 00:08:30,990 --> 00:08:36,070 we're going to return and underscore so for example if we take the diff between 135 00:08:36,070 --> 00:08:37,250 haskell 136 00:08:37,250 --> 00:08:40,500 and Batgirl then we see that 137 00:08:40,500 --> 00:08:43,070 the a ass 138 00:08:43,070 --> 00:08:45,560 appears here and a double al 139 00:08:45,560 --> 00:08:49,600 appears here because L appears and their 140 00:08:49,600 --> 00:08:52,830 but we see it twice because you are you know we just look whether 141 00:08:52,830 --> 00:08:56,140 L appears in this string yes and then we see another al 142 00:08:56,140 --> 00:08:59,660 that appears here as well so it's a 143 00:08:59,660 --> 00:09:03,250 a very simple David just checks whether the strange 144 00:09:03,250 --> 00:09:08,089 registering on the left appear on the right alright 145 00:09:08,089 --> 00:09:12,510 that was %uh the whole game happy hacking and make sure again 146 00:09:12,510 --> 00:09:15,610 that you do your exercises an 147 00:09:15,610 --> 00:09:20,140 and that you play around with the i/o monette and when you write code yourself 148 00:09:20,140 --> 00:09:23,810 no matter whether it's haskell C sharp 149 00:09:23,810 --> 00:09:28,470 job I at Sharp skyline bite on PHP 150 00:09:28,470 --> 00:09:32,620 think about the idea here up having a CEO 151 00:09:32,620 --> 00:09:36,079 impaired if God that interacts with the outside world 152 00:09:36,079 --> 00:09:39,269 and in there have Islands up your code 153 00:09:39,269 --> 00:09:42,420 where Roger functions in a pure Whey 154 00:09:42,420 --> 00:09:47,079 but yeah you have to decide how big the islands are 155 00:09:47,079 --> 00:09:52,470 and how big this yes and but the answer is never dead there's only Islands 156 00:09:52,470 --> 00:09:58,680 are only see a good programmer knows exactly the right balance great 157 00:09:58,680 --> 00:10:00,810 thank you and see you next time