1 00:00:01,240 --> 00:00:04,529 welcome back to the lectures on higher-order functions 2 00:00:04,529 --> 00:00:07,899 and we just looked at the 3 00:00:07,899 --> 00:00:11,889 mother of all higher are the functions over lists for our 4 00:00:11,889 --> 00:00:15,460 an and we will and this section 5 00:00:15,460 --> 00:00:19,880 read a few other standard a higher-order functions that are available 6 00:00:19,880 --> 00:00:23,279 and the Haskell library and 7 00:00:23,279 --> 00:00:28,519 one of the those functions is function composition and function composition is 8 00:00:28,519 --> 00:00:30,699 another fundamental operation 9 00:00:30,699 --> 00:00:34,890 in functional programming an functional programming as programming with 10 00:00:34,890 --> 00:00:35,630 functions 11 00:00:35,630 --> 00:00:38,660 and if we have functions we want to gonna 12 00:00:38,660 --> 00:00:41,730 combined two functions into another function 13 00:00:41,730 --> 00:00:46,530 just like addition takes two numbers and give this a new number 14 00:00:46,530 --> 00:00:50,200 we want to have a similar operator that takes two functions 15 00:00:50,200 --> 00:00:53,980 and combines them into a new French well 16 00:00:53,980 --> 00:00:57,559 here's the and definition of that operator 17 00:00:57,559 --> 00:01:01,559 and it's written as dot and that's cool so unlike 18 00:01:01,559 --> 00:01:06,119 object-oriented programming where dot means invoking the method 19 00:01:06,119 --> 00:01:09,930 and has called dot means composing to function show 20 00:01:09,930 --> 00:01:13,470 in both cases the dot is used for the most 21 00:01:13,470 --> 00:01:17,360 fundamental operation on there gonna be a object 22 00:01:17,360 --> 00:01:20,990 that the language support an object-oriented language 23 00:01:20,990 --> 00:01:25,229 bf objects and we can call methods on them and Haskell 24 00:01:25,229 --> 00:01:28,710 we have functions that we can combo am 25 00:01:28,710 --> 00:01:32,700 and here's the type and function composition dot 26 00:01:32,700 --> 00:01:37,570 takes a function from BTC there's the function for b2c 27 00:01:37,570 --> 00:01:41,950 and it takes a function from a to be and that readers a function from 28 00:01:41,950 --> 00:01:45,229 a2c now when you look at this 29 00:01:45,229 --> 00:01:48,450 type it can be a little bit confusing 30 00:01:48,450 --> 00:01:51,820 biggest there's a bee here as the output 31 00:01:51,820 --> 00:01:56,460 and the bee is here as the input cell it's gonna be a 32 00:01:56,460 --> 00:02:00,630 seems to be that things are in the wrong order but debt has 33 00:02:00,630 --> 00:02:03,680 everything to do with the fact that 34 00:02:03,680 --> 00:02:07,899 function application shosh age so here's a definition 35 00:02:07,899 --> 00:02:10,950 of composition so what we do is read 36 00:02:10,950 --> 00:02:12,720 two functions and then 37 00:02:12,720 --> 00:02:16,050 we first applied G to axe 38 00:02:16,050 --> 00:02:19,650 and then reapply after to the result 39 00:02:19,650 --> 00:02:22,900 case allege now see 40 00:02:22,900 --> 00:02:26,209 if those types make sense to us an 41 00:02:26,209 --> 00:02:29,280 the title that here 42 00:02:29,280 --> 00:02:32,819 is a function from B to C the typeof 43 00:02:32,819 --> 00:02:36,940 G is a function from a to be well 44 00:02:36,940 --> 00:02:40,680 then in that case X must be a value of type A 45 00:02:40,680 --> 00:02:44,230 so if we apply G to at 46 00:02:44,230 --> 00:02:50,239 to axe we have we are blind dysfunction addiction a.m. 20 NB 47 00:02:50,239 --> 00:02:53,640 so the result of applying G two axes be 48 00:02:53,640 --> 00:02:58,000 hi and that fits into here and we get to see 49 00:02:58,000 --> 00:03:01,150 show to get the 50 00:03:01,150 --> 00:03:04,250 return type here you get the value of type A 51 00:03:04,250 --> 00:03:07,610 and we return a value of type C 52 00:03:07,610 --> 00:03:13,530 alright so at first I thought might look a little bit confusing 53 00:03:13,530 --> 00:03:17,829 but once we figure it out and got you know we used in fact that 54 00:03:17,829 --> 00:03:21,090 function application here shosh age to deride 55 00:03:21,090 --> 00:03:24,459 everything's Folge falls into place I'm 56 00:03:24,459 --> 00:03:28,230 and here's an example %uh using function composition 57 00:03:28,230 --> 00:03:31,780 suppose we want to define the function alt that 58 00:03:31,780 --> 00:03:34,959 checks whether a an integer is art well 59 00:03:34,959 --> 00:03:38,040 alt is defined as not even 60 00:03:38,040 --> 00:03:41,250 well there we go definition it's at 61 00:03:41,250 --> 00:03:44,989 first check whether g++ even and then an 62 00:03:44,989 --> 00:03:49,989 inverter result and this definition here 63 00:03:49,989 --> 00:03:53,030 this is what people often call point freestyle 64 00:03:53,030 --> 00:03:56,380 biggest the other way we could ride at a.m. 65 00:03:56,380 --> 00:04:01,280 alt would be love blacks even %uh axe 66 00:04:01,280 --> 00:04:05,980 not of that but now we have a long time brent is she said that's got kind of a 67 00:04:05,980 --> 00:04:07,070 little bit for books 68 00:04:07,070 --> 00:04:11,010 and a lot of functional programmers preferred to you should function 69 00:04:11,010 --> 00:04:12,079 composition 70 00:04:12,079 --> 00:04:17,519 to remove all the lawn that's and the brain disease but I have to warn you 71 00:04:17,519 --> 00:04:20,989 and and I'm I'm guilty of this too 72 00:04:20,989 --> 00:04:25,470 when you just start using function applique a French 73 00:04:25,470 --> 00:04:29,280 in you're trying to use it everywhere and it becomes a sport 74 00:04:29,280 --> 00:04:32,990 to write things in point freestyle ride them very compactly 75 00:04:32,990 --> 00:04:37,780 an the price you pay said becomes very hard to reach and now the people that 76 00:04:37,780 --> 00:04:38,900 Reacher code 77 00:04:38,900 --> 00:04:42,540 have to do all this function composition 78 00:04:42,540 --> 00:04:45,780 in order to understand what's going on and typically 79 00:04:45,780 --> 00:04:49,430 when they understand what's going on the light bulb goes on it 80 00:04:49,430 --> 00:04:53,450 okay but I think it's bad either to gonna Lu's 81 00:04:53,450 --> 00:04:56,820 function composition sparingly an 82 00:04:56,820 --> 00:05:00,640 because you always have to write code for others to read 83 00:05:00,640 --> 00:05:05,430 okay so you don't write code to look cool yourself and see all ok I can do 84 00:05:05,430 --> 00:05:06,790 this point freestyle 85 00:05:06,790 --> 00:05:11,690 am plus there's our domestic programs that make things point free for you show 86 00:05:11,690 --> 00:05:13,390 your not as cool as you think 87 00:05:13,390 --> 00:05:17,060 okay so use function composition sparingly 88 00:05:17,060 --> 00:05:21,169 and don't go bananas wit function composition 89 00:05:21,169 --> 00:05:26,330 okay 90 00:05:26,330 --> 00:05:29,960 here are a couple of a other functions 91 00:05:29,960 --> 00:05:33,890 and that are in the standard library higher-order functions 92 00:05:33,890 --> 00:05:37,890 am these are most of them are defined 93 00:05:37,890 --> 00:05:42,000 and using and for the are using 94 00:05:42,000 --> 00:05:46,620 and comprehension these are all interchangeably 95 00:05:46,620 --> 00:05:49,770 and I prefer using fold our 96 00:05:49,770 --> 00:05:53,750 and some people a briefer an 97 00:05:53,750 --> 00:05:57,760 this comprehension ce at the reason I prefer for the car is dead 98 00:05:57,760 --> 00:06:02,510 for are and this concept of folding over and 99 00:06:02,510 --> 00:06:06,280 a datatype works for any recursive data type 100 00:06:06,280 --> 00:06:11,790 birch list comprehension am only work for less 101 00:06:11,790 --> 00:06:16,350 an we can use general branches but it gets all a bit tedious 102 00:06:16,350 --> 00:06:19,370 so I at some point it's just easier to 103 00:06:19,370 --> 00:06:22,380 define default function and and the fine 104 00:06:22,380 --> 00:06:26,100 and other functions on top of that all right sir 105 00:06:26,100 --> 00:06:29,840 but let's just look at this wrong here all is defined 106 00:06:29,840 --> 00:06:34,210 as and map 107 00:06:34,210 --> 00:06:37,150 be a VAX to the list access 108 00:06:37,150 --> 00:06:41,960 and then and Jack rather every one of those is true 109 00:06:41,960 --> 00:06:45,789 measure a nicer definition of dish would be 110 00:06:45,789 --> 00:06:50,710 map of P combos and any 111 00:06:50,710 --> 00:06:54,500 is the same thing remapped be a fax I P 112 00:06:54,500 --> 00:06:57,620 over the list access and then we call 113 00:06:57,620 --> 00:07:01,180 or so for example if you want to check that 114 00:07:01,180 --> 00:07:04,580 this string here ABC space deef 115 00:07:04,580 --> 00:07:08,360 days in space you can call any Espace 116 00:07:08,360 --> 00:07:12,240 function from character to bull and this will return true 117 00:07:12,240 --> 00:07:16,199 here's another interesting function 118 00:07:16,199 --> 00:07:19,580 take while take while takes a predicate 119 00:07:19,580 --> 00:07:24,300 and unlike filter rich goes through the whole list and throws away all the 120 00:07:24,300 --> 00:07:25,150 elements 121 00:07:25,150 --> 00:07:29,340 for rates abroad again does not halt take while 122 00:07:29,340 --> 00:07:32,580 is like a while loop so it goes uses predicate 123 00:07:32,580 --> 00:07:36,690 and chop shop elements from this list and returning them 124 00:07:36,690 --> 00:07:41,039 until this predicate this fall so let's look at the definition 125 00:07:41,039 --> 00:07:46,460 if the list is empty well what can we do we can only return the empty list 126 00:07:46,460 --> 00:07:49,810 in case you be have a non-empty lest 127 00:07:49,810 --> 00:07:53,830 it looks like this ex-cons excess 128 00:07:53,830 --> 00:07:56,960 and now we have two cases either 129 00:07:56,960 --> 00:08:00,759 the affects does not halt ever that case we 130 00:08:00,759 --> 00:08:04,169 and terminates the recursion by returning the anti lest 131 00:08:04,169 --> 00:08:07,750 and ft up if the attacks is true 132 00:08:07,750 --> 00:08:11,210 there's a trap door here and in the stadia 133 00:08:11,210 --> 00:08:15,219 take while not fault through the 134 00:08:15,219 --> 00:08:18,529 all an FB have access through 135 00:08:18,529 --> 00:08:22,940 then we Goldman Sachs onto the recursive call 136 00:08:22,940 --> 00:08:26,909 of P take while be affects so for example 137 00:08:26,909 --> 00:08:30,630 began they have a string and we can take 138 00:08:30,630 --> 00:08:34,719 and characters from the string as long as they are 139 00:08:34,719 --> 00:08:38,570 letters by the intake while is all fun and then 140 00:08:38,570 --> 00:08:43,230 it will take ABC and since this space is not 141 00:08:43,230 --> 00:08:46,820 and an often America character as well terminated 142 00:08:46,820 --> 00:08:51,250 right there do I need to take while 143 00:08:51,250 --> 00:08:55,840 we have the function dropped while that removes all the elements 144 00:08:55,840 --> 00:08:59,750 while a predicate holes show take while 145 00:08:59,750 --> 00:09:02,840 keeps all the elements until the predicate is false 146 00:09:02,840 --> 00:09:06,410 drop while drops the element and 147 00:09:06,410 --> 00:09:10,440 until the predicate at turns falls 148 00:09:10,440 --> 00:09:14,820 so you see here that an in this case it drops the value 149 00:09:14,820 --> 00:09:18,880 and and said every during the ante LIJ there it just got 150 00:09:18,880 --> 00:09:21,940 yeah which the the valley on top of the rest 151 00:09:21,940 --> 00:09:25,440 good happy hacking 152 00:09:25,440 --> 00:09:28,570 and make sure you do the exercises because 153 00:09:28,570 --> 00:09:32,550 all the higher branches that we have seen here like take wild ride while map 154 00:09:32,550 --> 00:09:33,310 filter 155 00:09:33,310 --> 00:09:37,670 and you will have to implement them in terms of old R 156 00:09:37,670 --> 00:09:42,070 a ride and so please make these exercises 157 00:09:42,070 --> 00:09:47,510 and if you're not using haskell an say you're using Scala our job on 158 00:09:47,510 --> 00:09:50,580 you can do the same thing these languages typically 159 00:09:50,580 --> 00:09:54,690 also have fold our and maybe it's not called for 160 00:09:54,690 --> 00:09:58,720 are sometimes is called fourth ride or sometimes just called fault 161 00:09:58,720 --> 00:10:03,210 but this idea of having a higher r2 function 162 00:10:03,210 --> 00:10:07,030 that captures the ass and shove rick urging over lest 163 00:10:07,030 --> 00:10:10,240 is available and and the library 164 00:10:10,240 --> 00:10:13,580 an that that supports lists 165 00:10:13,580 --> 00:10:17,150 and for example in c-sharp it's not even cold fold our 166 00:10:17,150 --> 00:10:22,270 it's cold San aggregate 167 00:10:22,270 --> 00:10:26,460 so that comes from Chico lawyer aggregating over list 168 00:10:26,460 --> 00:10:30,950 so don't be fooled by the name and the idea is what matters 169 00:10:30,950 --> 00:10:33,760 and thank you so much and see you next week