1 00:00:01,430 --> 00:00:03,679 welcome back everybody to 2 00:00:03,679 --> 00:00:07,310 the Anna lecture to 3 00:00:07,310 --> 00:00:10,550 about types and clash and 4 00:00:10,550 --> 00:00:14,009 this part we're going to talk about types of 5 00:00:14,009 --> 00:00:18,750 functions and since we are dealing with functional programming 6 00:00:18,750 --> 00:00:22,050 and functions values Antibes 7 00:00:22,050 --> 00:00:25,510 are very important and since this 8 00:00:25,510 --> 00:00:29,730 can be a little bit and difficult for beginners 9 00:00:29,730 --> 00:00:34,120 police pay attention to this lecture in particular biggest 10 00:00:34,120 --> 00:00:37,760 if you missed this you will have difficulties 11 00:00:37,760 --> 00:00:42,809 in all their remaining lecture so and I will go a little bit slow here 12 00:00:42,809 --> 00:00:46,550 and repeat many times because this is super important 13 00:00:46,550 --> 00:00:50,960 that you understand this part monitor function 14 00:00:50,960 --> 00:00:54,510 a function is a mapping from values 15 00:00:54,510 --> 00:00:58,210 of one type to values of another time show here too 16 00:00:58,210 --> 00:01:01,879 very simple examples a function types the first one 17 00:01:01,879 --> 00:01:06,869 is the function not ligation which takes Boolean 18 00:01:06,869 --> 00:01:10,030 and breeders in Other Boleyn an 19 00:01:10,030 --> 00:01:13,530 not to nothing to end complicated 20 00:01:13,530 --> 00:01:16,790 let's look at the second function here is digit 21 00:01:16,790 --> 00:01:20,130 is digit takes a character and checks 22 00:01:20,130 --> 00:01:25,420 writer that character is digit show whether that character is 0-1 23 00:01:25,420 --> 00:01:30,090 up to nine the diver that is chargeable shell 24 00:01:30,090 --> 00:01:34,439 what that means is it takes a value in the shower all characters 25 00:01:34,439 --> 00:01:38,220 and will return a value in this ad of booleans 26 00:01:38,220 --> 00:01:43,159 and in general a function from t1 to t2 27 00:01:43,159 --> 00:01:48,509 is a the shark dive team on a roadie to 28 00:01:48,509 --> 00:01:51,540 is the type of functions that map values 29 00:01:51,540 --> 00:01:54,630 from typed the wrong two values for 30 00:01:54,630 --> 00:01:58,360 dude of type t2 the one ish 31 00:01:58,360 --> 00:02:01,909 also sometimes called the domain of the function 32 00:02:01,909 --> 00:02:05,430 and d2 is sometimes called their range 33 00:02:05,430 --> 00:02:07,460 the function 34 00:02:07,460 --> 00:02:11,820 now you noticed that and free rides 35 00:02:11,820 --> 00:02:15,150 the arrow of the function dive as a nice arrow 36 00:02:15,150 --> 00:02:20,500 but when you use the key part and to type in the actual code 37 00:02:20,500 --> 00:02:24,480 you will type that as a dash followed by a greater than sign 38 00:02:24,480 --> 00:02:29,340 and but we will use it can I search index here but just you 39 00:02:29,340 --> 00:02:32,900 be careful this is not you know how you type in 40 00:02:32,900 --> 00:02:38,020 actually the arrow when you have a function 41 00:02:38,020 --> 00:02:41,910 just like grits the doubles embed lists 42 00:02:41,910 --> 00:02:45,030 that typed for the range in the domain are 43 00:02:45,030 --> 00:02:48,220 unrestricted for example we can ever function here 44 00:02:48,220 --> 00:02:51,450 at that takes as the argument a top all 45 00:02:51,450 --> 00:02:56,040 of two integers and returns as a result another integer 46 00:02:56,040 --> 00:03:01,210 so at up X camara y equals x plus why so you see here that 47 00:03:01,210 --> 00:03:05,330 at takes it up all to of two integers 48 00:03:05,330 --> 00:03:10,150 and returns a result that is an integer and the other French in here 49 00:03:10,150 --> 00:03:15,600 an is a function that takes and then and returns a list of integers 50 00:03:15,600 --> 00:03:18,940 I'm perfectly fine and 51 00:03:18,940 --> 00:03:23,040 definition their $0.02 an readers the list 52 00:03:23,040 --> 00:03:27,500 0 dot dot and we have seen in the very first lecture 53 00:03:27,500 --> 00:03:31,420 that this square bracket with the an 54 00:03:31,420 --> 00:03:35,239 double dots denote the list from zero up to an 55 00:03:35,239 --> 00:03:39,900 one thing 56 00:03:39,900 --> 00:03:43,959 every make the types 57 00:03:43,959 --> 00:03:47,510 did arguments and result function types 58 00:03:47,510 --> 00:03:52,989 arbitrary they can be functions themselves and this is where 59 00:03:52,989 --> 00:03:57,110 current functions common so instead of defining at 60 00:03:57,110 --> 00:04:00,790 to take double-a integers and return integer 61 00:04:00,790 --> 00:04:04,850 and has college more common to define at as follows: 62 00:04:04,850 --> 00:04:09,380 we define at as a function that takes an integer 63 00:04:09,380 --> 00:04:14,010 and readership function that takes another integer 64 00:04:14,010 --> 00:04:17,169 and returns an integer so 65 00:04:17,169 --> 00:04:20,979 look at this type here so at is a function 66 00:04:20,979 --> 00:04:23,979 here we see that from this era 67 00:04:23,979 --> 00:04:28,009 that takes an integer and returns a function 68 00:04:28,009 --> 00:04:31,889 that takes another integer and readers an integer 69 00:04:31,889 --> 00:04:36,430 said that is curry now 70 00:04:36,430 --> 00:04:39,430 by itself carrying is not 71 00:04:39,430 --> 00:04:44,129 very complicated if you need you now there functions are just 72 00:04:44,129 --> 00:04:48,159 values like list an integer show there's nothing special about this 73 00:04:48,159 --> 00:04:52,330 the thing words becomes confusing is that 74 00:04:52,330 --> 00:04:55,729 in Haskell you don't have to ride dish arrows here 75 00:04:55,729 --> 00:04:59,539 alright because this is so common and 76 00:04:59,539 --> 00:05:02,990 the arrow associates to deride 77 00:05:02,990 --> 00:05:06,080 and which means that you can write this diverse end 78 00:05:06,080 --> 00:05:11,370 arrow and arrow and and that's where it becomes confusing for began 79 00:05:11,370 --> 00:05:15,710 show you have to really gonna you know when you read these types know where to 80 00:05:15,710 --> 00:05:16,520 put the brand 81 00:05:16,520 --> 00:05:20,599 she's but for now it you now there's nothing special here 82 00:05:20,599 --> 00:05:25,460 at is a function that takes an integer and readers another function that takes 83 00:05:25,460 --> 00:05:29,529 an integer and then returns an integer again 84 00:05:29,529 --> 00:05:32,639 now noticed that both at 85 00:05:32,639 --> 00:05:36,889 and at prime return the same result 86 00:05:36,889 --> 00:05:39,919 okay but at takes the 87 00:05:39,919 --> 00:05:44,180 the normal at stake should %uh polisher takes both arguments at the same time 88 00:05:44,180 --> 00:05:48,159 where's at Brian takes the argument 89 00:05:48,159 --> 00:05:51,199 one by one and arguments that they can 90 00:05:51,199 --> 00:05:54,860 that punches take their arguments one by one are called 91 00:05:54,860 --> 00:05:58,300 current functions an and 92 00:05:58,300 --> 00:06:02,009 the person that invented 93 00:06:02,009 --> 00:06:07,150 this an way of dealing with functions where the functions take the arguments 94 00:06:07,150 --> 00:06:08,050 one by one 95 00:06:08,050 --> 00:06:11,449 boss has Kobe curry and Haskell 96 00:06:11,449 --> 00:06:17,439 is named after him so that's where has gone from just like Pascal 97 00:06:17,439 --> 00:06:20,990 and was named after blaise pascal 98 00:06:20,990 --> 00:06:24,509 Haskell is named after haskell be curry 99 00:06:24,509 --> 00:06:30,420 and the person that invented carrying of functions 100 00:06:30,420 --> 00:06:32,800 every function that takes two arguments: 101 00:06:32,800 --> 00:06:38,360 can be turned into a function that takes these arguments one by one 102 00:06:38,360 --> 00:06:42,780 for example here's a function multi that takes three 103 00:06:42,780 --> 00:06:46,530 parameters XY and z. and multiply stem some old 104 00:06:46,530 --> 00:06:49,620 XYZ is X time July dime si 105 00:06:49,620 --> 00:06:53,870 one way to ride this function would be to take a top all 106 00:06:53,870 --> 00:06:57,300 three values XY and z. and then multiplied 107 00:06:57,300 --> 00:07:02,190 but the Haskell way of doing this is to take these arguments one by one 108 00:07:02,190 --> 00:07:06,190 so mold axe readers a function 109 00:07:06,190 --> 00:07:10,000 that takes another argument why and then 110 00:07:10,000 --> 00:07:13,070 its readers a function that takes another argument see 111 00:07:13,070 --> 00:07:17,180 and then why you have all these three arguments you can multiply them 112 00:07:17,180 --> 00:07:20,380 to return that and final and 113 00:07:20,380 --> 00:07:24,050 okay so moult takes an integer 114 00:07:24,050 --> 00:07:28,240 readers a function multi axe addiction and azure why 115 00:07:28,240 --> 00:07:33,440 that readers a new function mold actual I that takes into Jersey 116 00:07:33,440 --> 00:07:36,570 and readers the result XY and z. 117 00:07:36,570 --> 00:07:39,730 now what we see here is that 118 00:07:39,730 --> 00:07:44,770 and it died we still ride to Brenda she showed a brand that she's in the type 119 00:07:44,770 --> 00:07:48,930 go left to right so function the function arrow 120 00:07:48,930 --> 00:07:52,150 binds to the right but 121 00:07:52,150 --> 00:07:58,350 when we ride here and nafta gonna go here when we write mold 122 00:07:58,350 --> 00:08:01,419 XY we don't put branch 3.2 per 123 00:08:01,419 --> 00:08:05,320 would put a branch there you would ride mold axe 124 00:08:05,320 --> 00:08:10,130 Iran's and then branch around that you there the brand she's 125 00:08:10,130 --> 00:08:14,080 associate to the left a chauffeur application 126 00:08:14,080 --> 00:08:18,620 the parentheses show she ate to the left for the function arrow 127 00:08:18,620 --> 00:08:24,100 they are associate to the right and that matches up perfectly show then 128 00:08:24,100 --> 00:08:27,260 in practice we don't have to write and the brand 129 00:08:27,260 --> 00:08:30,710 now you can ask yourself 130 00:08:30,710 --> 00:08:33,729 why is this guy really useful okay 131 00:08:33,729 --> 00:08:36,830 why are we just like in other languages 132 00:08:36,830 --> 00:08:38,340 why don't we just 133 00:08:38,340 --> 00:08:43,300 have functions that take a whole list of parameters are 134 00:08:43,300 --> 00:08:46,980 like top of parameters to be precise in one go 135 00:08:46,980 --> 00:08:51,920 why do we do this complicated thing of God taking one our game and and then 136 00:08:51,920 --> 00:08:54,240 returning a function that takes the other 137 00:08:54,240 --> 00:08:58,200 parameter et cetera well the reason is 138 00:08:58,200 --> 00:09:01,290 that we can partially apply a function 139 00:09:01,290 --> 00:09:05,570 for example if we have this function at Brian 140 00:09:05,570 --> 00:09:10,280 that was defined in a curried way we can define a function that 141 00:09:10,280 --> 00:09:15,220 incur a man's fill you with Wong by partially applying 142 00:09:15,220 --> 00:09:19,390 at crime to wong so if we look at that type there 143 00:09:19,390 --> 00:09:23,720 at prime Wong is a function that takes an integer 144 00:09:23,720 --> 00:09:28,960 everyday urge another integer when we will talk about last 145 00:09:28,960 --> 00:09:33,890 in the future lecture we'll see that there's many functions 146 00:09:33,890 --> 00:09:37,940 that transform lists into other lest 147 00:09:37,940 --> 00:09:42,260 and Moshe dysfunctions are also defined in the current form 148 00:09:42,260 --> 00:09:45,830 for example the function take here take five 149 00:09:45,830 --> 00:09:50,530 and take is defined as a current function 150 00:09:50,530 --> 00:09:54,970 that takes an integer readers a function from ledge to list 151 00:09:54,970 --> 00:09:58,220 which may change that we can partially applied 152 00:09:58,220 --> 00:10:01,740 take 25 to yield a function 153 00:10:01,740 --> 00:10:04,960 that takes a list of integers to another list of integers 154 00:10:04,960 --> 00:10:08,540 which just takes the first five Allemands 155 00:10:08,540 --> 00:10:12,790 of the input list similarly the function drop 156 00:10:12,790 --> 00:10:16,960 takes an integer and readers a function from less two lists 157 00:10:16,960 --> 00:10:20,720 and what we see here is that we can now define dropped five 158 00:10:20,720 --> 00:10:25,570 we can partially applied drop 25 and it will return a function 159 00:10:25,570 --> 00:10:29,200 that will take a list dropped the first five elements 160 00:10:29,200 --> 00:10:32,260 and then returned should this is super convenient 161 00:10:32,260 --> 00:10:35,650 and and an 162 00:10:35,650 --> 00:10:39,350 its in Haskell it's very lightweight you disinfection haskell 163 00:10:39,350 --> 00:10:42,610 is optimized to have carried functions 164 00:10:42,610 --> 00:10:47,370 and in that respect haskell is very very different from 165 00:10:47,370 --> 00:10:51,950 most other languages that are optimized for functions that take 166 00:10:51,950 --> 00:10:57,830 to boss a bargain I already mentioned a couple of times 167 00:10:57,830 --> 00:11:02,140 the way we do and the breadth rent a seizure on 168 00:11:02,140 --> 00:11:05,790 and expressions with function parameters so the 169 00:11:05,790 --> 00:11:08,860 arrow associates to the right so if we have the 170 00:11:08,860 --> 00:11:11,970 and the their type 171 00:11:11,970 --> 00:11:15,880 on the bottom day here so the type here into arrow and 172 00:11:15,880 --> 00:11:19,760 arrow in Tarrant the brand she's our 173 00:11:19,760 --> 00:11:24,220 showcase do the ride and says we don't like to write parentheses 174 00:11:24,220 --> 00:11:28,360 began ride this thing as indeed arrow in Tarrant arrow in 175 00:11:28,360 --> 00:11:32,310 okay but if we put all the practices and their 176 00:11:32,310 --> 00:11:36,760 we get the term here at the bottom but this is what we gonna ride 177 00:11:36,760 --> 00:11:40,750 and I guess we we don't want a ride 178 00:11:40,750 --> 00:11:45,950 unnecessary for entities and as I said all saw 179 00:11:45,950 --> 00:11:49,710 when we do function application the 180 00:11:49,710 --> 00:11:53,270 and Brenda sheesh Associated the left 181 00:11:53,270 --> 00:11:58,430 so when we partially apply a function you can write Brent this is our on dad 182 00:11:58,430 --> 00:12:02,250 but we don't have to chill when you write mold XYZ 183 00:12:02,250 --> 00:12:06,230 that really means mall tax applied 184 00:12:06,230 --> 00:12:10,300 to why apply to G okay 185 00:12:10,300 --> 00:12:14,450 but an unless 186 00:12:14,450 --> 00:12:17,450 and Angela unless you're using explicit 187 00:12:17,450 --> 00:12:22,110 the bus all functions haskell are normally defined 188 00:12:22,110 --> 00:12:26,250 encourage for an an most languages 189 00:12:26,250 --> 00:12:29,470 all functions are defined in 190 00:12:29,470 --> 00:12:33,500 and the encourage for so I'll 191 00:12:33,500 --> 00:12:37,690 dish the part 2 and about functions there will be 192 00:12:37,690 --> 00:12:44,440 a lot of exercises to drill this idea of you know where to put the brand she's 193 00:12:44,440 --> 00:12:48,270 in the and function type 194 00:12:48,270 --> 00:12:51,580 and where to put the brand she's in the expressions 195 00:12:51,580 --> 00:12:56,090 and so this become second nature is this something you really have to 196 00:12:56,090 --> 00:13:00,580 gonna keep doing until you don't think about it then 197 00:13:00,580 --> 00:13:04,620 alright so an please do your exercises 198 00:13:04,620 --> 00:13:08,490 and see you later and in the last part 199 00:13:08,490 --> 00:13:09,510 of Chapter two