1 00:00:00,480 --> 00:00:03,120 welcome to this jam session 2 00:00:03,120 --> 00:00:07,450 and then this jam session I'm going to show you 3 00:00:07,450 --> 00:00:11,750 how the principles functional programming that we've seen 4 00:00:11,750 --> 00:00:15,150 in the course for Haskell can be applied 5 00:00:15,150 --> 00:00:18,590 a.m. to their language cop Gotland 6 00:00:18,590 --> 00:00:22,470 is a language designed to buy JetBrains 7 00:00:22,470 --> 00:00:25,859 and if you're in and ellijay user like I am 8 00:00:25,859 --> 00:00:29,890 and you can and download the plugin for cropland 9 00:00:29,890 --> 00:00:33,590 and and play with a child let's go and messy 10 00:00:33,590 --> 00:00:37,250 how we can do functional programming in 11 00:00:37,250 --> 00:00:40,340 a a normal language an 12 00:00:40,340 --> 00:00:43,989 here is the Anna and main 13 00:00:43,989 --> 00:00:48,000 function for our call in program just like 14 00:00:48,000 --> 00:00:51,579 job on sharper than made 15 00:00:51,579 --> 00:00:54,809 takes array of strings as an argument and 16 00:00:54,809 --> 00:00:57,879 inside the body we see a couple of expressions here 17 00:00:57,879 --> 00:01:01,739 and I want to walk you through them and and show you 18 00:01:01,739 --> 00:01:05,010 how this relates today and Haskell code 19 00:01:05,010 --> 00:01:09,040 that we have seen and notes that for me 20 00:01:09,040 --> 00:01:12,689 functional programming does not imply 21 00:01:12,689 --> 00:01:17,540 and not using side effects functional programming does not mean 22 00:01:17,540 --> 00:01:22,210 using pattern matching for me functional programming means 23 00:01:22,210 --> 00:01:25,909 using higher-order functions whether they're you're not 24 00:01:25,909 --> 00:01:29,420 an and focusing on expressions 25 00:01:29,420 --> 00:01:34,600 various statements and got then you can define you to both they're both so I can 26 00:01:34,600 --> 00:01:35,310 define 27 00:01:35,310 --> 00:01:38,610 var and why and 28 00:01:38,610 --> 00:01:42,509 initialized tu wong and then I can say why 29 00:01:42,509 --> 00:01:46,430 and plus equals Wong 30 00:01:46,430 --> 00:01:50,240 and this just work and I have no problem at all with that 31 00:01:50,240 --> 00:01:54,430 okay but what I wanted to show here 32 00:01:54,430 --> 00:01:57,759 is and the functional programming features 33 00:01:57,759 --> 00:02:02,280 that are supported by cop so let's look at this for statement here so I define 34 00:02:02,280 --> 00:02:06,869 a an immutable variable here are the 35 00:02:06,869 --> 00:02:10,369 just a name a square and that goals 36 00:02:10,369 --> 00:02:14,400 the an function foo and Fu is defiant 37 00:02:14,400 --> 00:02:17,520 here as a function you see the and 38 00:02:17,520 --> 00:02:21,849 if I over over this it and that may be a little bit 39 00:02:21,849 --> 00:02:27,290 small to see I'm on the screen but and got then has type inference for the 40 00:02:27,290 --> 00:02:28,739 return type show here 41 00:02:28,739 --> 00:02:32,060 it said Tekken that's very small yellow thing that you 42 00:02:32,060 --> 00:02:35,220 currency that the type of 43 00:02:35,220 --> 00:02:38,569 through and is a function that they can 44 00:02:38,569 --> 00:02:42,599 and & Returns & and here I 45 00:02:42,599 --> 00:02:47,190 call through a forty-two and I printed now another thing that we've seen in 46 00:02:47,190 --> 00:02:47,989 Haskell 47 00:02:47,989 --> 00:02:52,050 and the less comprehension ce work these range expressions where we 48 00:02:52,050 --> 00:02:55,310 we could ride in Haskell square brackets Wong dot dot 49 00:02:55,310 --> 00:03:00,819 am an got and support something very similar so I can say for action 50 00:03:00,819 --> 00:03:04,050 at the range 125 print X 51 00:03:04,050 --> 00:03:07,860 so here you see that this shame gonna syntax and I the years 52 00:03:07,860 --> 00:03:11,480 range comprehension appear 53 00:03:11,480 --> 00:03:15,519 in and other languages as well now 54 00:03:15,519 --> 00:03:19,379 the next example shows 55 00:03:19,379 --> 00:03:25,459 am some higher order functions and the syntax that got land uses for them to 56 00:03:25,459 --> 00:03:25,900 hear 57 00:03:25,900 --> 00:03:30,220 I declare lest a list of the values 1234 58 00:03:30,220 --> 00:03:33,540 and then I mapped the function a 59 00:03:33,540 --> 00:03:37,500 I rode along that expression a a row eight times to over it 60 00:03:37,500 --> 00:03:40,590 and then I filter out all the values 61 00:03:40,590 --> 00:03:45,190 less than five and you see that there's two ways to define 62 00:03:45,190 --> 00:03:49,549 along the expression the first one here is where you name the 63 00:03:49,549 --> 00:03:54,430 arguments and arrow and then the expression body 64 00:03:54,430 --> 00:03:57,980 and the longer is enclosed in curly braces 65 00:03:57,980 --> 00:04:01,379 you can leave out the 66 00:04:01,379 --> 00:04:04,610 an Iran's around the map 67 00:04:04,610 --> 00:04:09,239 but I can have liked that to put them there 68 00:04:09,239 --> 00:04:12,569 explicitly and hear the filter is seeded if 69 00:04:12,569 --> 00:04:17,049 a long the expression just takes one argument we don't have to name it 70 00:04:17,049 --> 00:04:20,410 and we just call it it's very similar to 71 00:04:20,410 --> 00:04:24,250 when you have an object that the and implicit 72 00:04:24,250 --> 00:04:27,290 context that this parameter is called this 73 00:04:27,290 --> 00:04:30,290 when you have a function with one parameter 74 00:04:30,290 --> 00:04:34,430 it make sense to call that one parameter give it a special name 75 00:04:34,430 --> 00:04:37,680 such that you have to write less 76 00:04:37,680 --> 00:04:41,040 okay sure you can do everything that you learned in the course 77 00:04:41,040 --> 00:04:44,130 with with maps filter fault an 78 00:04:44,130 --> 00:04:47,610 you can do in Cortland as well and 79 00:04:47,610 --> 00:04:51,800 is he here there's I'll all the functions 80 00:04:51,800 --> 00:04:55,050 defined on lists that you will recognize 81 00:04:55,050 --> 00:04:58,639 from the Haskell brilliant most programming languages if you look at the 82 00:04:58,639 --> 00:04:59,710 API 83 00:04:59,710 --> 00:05:03,910 for collections they are very familiar and they all contain 84 00:05:03,910 --> 00:05:07,169 and the same Iran function sometimes 85 00:05:07,169 --> 00:05:10,660 names are slightly different different but in general 86 00:05:10,660 --> 00:05:14,690 and that you'd be very familiar 87 00:05:14,690 --> 00:05:18,110 one nice thing and that got land has 88 00:05:18,110 --> 00:05:21,900 and which C sharp also has our extension that its 89 00:05:21,900 --> 00:05:25,990 so in this case I define an extension that at some 90 00:05:25,990 --> 00:05:30,150 on integer let's look at and the definition for 91 00:05:30,150 --> 00:05:33,699 the some extension at issue here you see and defining 92 00:05:33,699 --> 00:05:36,820 and a love the expression 93 00:05:36,820 --> 00:05:40,190 and this into dots their 94 00:05:40,190 --> 00:05:43,190 indicates that some as an extension method 95 00:05:43,190 --> 00:05:47,389 on ent and it takes parameter logical that other 96 00:05:47,389 --> 00:05:50,630 and its returns and ensure here you see and the 97 00:05:50,630 --> 00:05:54,550 type and then the body of the 98 00:05:54,550 --> 00:05:57,550 I'm extension method just at this 99 00:05:57,550 --> 00:06:00,580 to other show in this case decision scope 100 00:06:00,580 --> 00:06:03,830 and an ranges over 101 00:06:03,830 --> 00:06:07,520 the an receiver and I think this is also 102 00:06:07,520 --> 00:06:12,850 and quite nice its less for boast and the way you define extension medicine C 103 00:06:12,850 --> 00:06:13,630 sharp 104 00:06:13,630 --> 00:06:17,990 very deftly finest static mat did you have to explicitly name the parameter 105 00:06:17,990 --> 00:06:21,569 dish modifier in front and this is 106 00:06:21,569 --> 00:06:24,669 very very concise and then we can just call 107 00:06:24,669 --> 00:06:29,199 Wong dot sum of two 108 00:06:29,199 --> 00:06:32,660 and lectures and when we talked about 109 00:06:32,660 --> 00:06:36,199 a defining data types I compared 110 00:06:36,199 --> 00:06:39,750 haskell at algebra datatypes with glasses 111 00:06:39,750 --> 00:06:43,169 in object-oriented languages and 112 00:06:43,169 --> 00:06:46,590 is word emphasizing that if you look 113 00:06:46,590 --> 00:06:50,740 at the course bonuses if you look at while the similar 114 00:06:50,740 --> 00:06:53,780 haskell als right data type is really 115 00:06:53,780 --> 00:06:56,789 kinda streamlined way to ride 116 00:06:56,789 --> 00:07:00,860 a class hierarchy and with constructors and strong 117 00:07:00,860 --> 00:07:06,990 and better matching is really a streamlined way to do is check 118 00:07:06,990 --> 00:07:10,690 let's see how we would be fine the type of lists 119 00:07:10,690 --> 00:07:14,169 in got lawn and how we can t find 120 00:07:14,169 --> 00:07:17,470 one of the standard triggers it functions over lists 121 00:07:17,470 --> 00:07:20,750 as an extension that'd using better matching 122 00:07:20,750 --> 00:07:24,169 or and est and then you will see 123 00:07:24,169 --> 00:07:28,740 that there's a little bit more syntax then in Haskell or maybe even the same 124 00:07:28,740 --> 00:07:30,500 amount of characters 125 00:07:30,500 --> 00:07:35,660 an but the important thing here is that you can take the concepts from Haskell 126 00:07:35,660 --> 00:07:40,270 algebra dated ives and apply them in an object or language 127 00:07:40,270 --> 00:07:44,229 and vice versa if you have inheritance hierarchy 128 00:07:44,229 --> 00:07:48,259 in an object turned language you can often mobile that also 129 00:07:48,259 --> 00:07:53,419 using haskell algebra datatypes 130 00:07:53,419 --> 00:07:57,199 here's a.m. the type 131 00:07:57,199 --> 00:08:00,220 a blessed so what I do is I D find 132 00:08:00,220 --> 00:08:04,669 abstract class list and this data modifier 133 00:08:04,669 --> 00:08:08,039 is a special modifier an that you can put 134 00:08:08,039 --> 00:08:11,210 on the government class that well and 135 00:08:11,210 --> 00:08:15,569 make sure that a bunch of things are implemented like equals hashCode and so 136 00:08:15,569 --> 00:08:16,360 on 137 00:08:16,360 --> 00:08:20,199 and you know that's rather easy 138 00:08:20,199 --> 00:08:24,479 and then I'd be fine to derived class issue in Haskell these would be on the 139 00:08:24,479 --> 00:08:27,910 right hand side of the algebraic datatype Chanel 140 00:08:27,910 --> 00:08:31,690 is a special case of alleged and college 141 00:08:31,690 --> 00:08:34,709 is a special occasion the list that as a hat 142 00:08:34,709 --> 00:08:38,890 of type T and a tail of typed list of tea 143 00:08:38,890 --> 00:08:43,899 again as you can see if you squint to read or you put on some different 144 00:08:43,899 --> 00:08:45,320 colored glasses 145 00:08:45,320 --> 00:08:48,620 this glass decoration is 146 00:08:48,620 --> 00:08:51,760 isomorphic to the Haskell definition for this 147 00:08:51,760 --> 00:08:55,019 one important difference of course is that and 148 00:08:55,019 --> 00:08:58,860 what then is a strict language so these are not lazy so if you want to do lazy 149 00:08:58,860 --> 00:08:59,730 list 150 00:08:59,730 --> 00:09:04,209 we have to do a little bit more work and I we have to make sure that 151 00:09:04,209 --> 00:09:09,200 an detail remains and evaluated and one way to do that 152 00:09:09,200 --> 00:09:12,550 is by creating a thank for that but thats 153 00:09:12,550 --> 00:09:17,130 topic for a different time now let's look at how we define 154 00:09:17,130 --> 00:09:20,500 and function recursively over 155 00:09:20,500 --> 00:09:25,010 this new list type an so here we define length 156 00:09:25,010 --> 00:09:29,790 and length is defined as an extension method over Leicester you see that 157 00:09:29,790 --> 00:09:33,240 list here has no Matt dense 158 00:09:33,240 --> 00:09:36,350 am but I just define them outside 159 00:09:36,350 --> 00:09:40,930 and here's my better match and I check when this is nel 160 00:09:40,930 --> 00:09:44,940 the result is 0 and I should lined it up properly 161 00:09:44,940 --> 00:09:48,740 so when adl the lest nil 162 00:09:48,740 --> 00:09:51,990 lactis 0 that's exactly like 163 00:09:51,990 --> 00:09:55,670 and has gone and when we have a college 164 00:09:55,670 --> 00:10:00,380 an what i do is it the result is one plus 165 00:10:00,380 --> 00:10:04,630 the length of the day love the list and so instead of 166 00:10:04,630 --> 00:10:08,079 better matching to get at detailed the list 167 00:10:08,079 --> 00:10:11,540 I am calling here the access your 168 00:10:11,540 --> 00:10:15,350 to get detail and since a goblin compiler 169 00:10:15,350 --> 00:10:18,720 and cannot determined that I'm not 170 00:10:18,720 --> 00:10:22,839 and extending this at data class anymore 171 00:10:22,839 --> 00:10:26,640 I have to put here this purist als loss an 172 00:10:26,640 --> 00:10:29,720 to draw an exception but we know that this will never happen 173 00:10:29,720 --> 00:10:33,010 but if if we do this if we don't do this and 174 00:10:33,010 --> 00:10:36,850 we will get an error here and say that arnelle's 175 00:10:36,850 --> 00:10:40,360 clause is required okay and then we can 176 00:10:40,360 --> 00:10:46,390 and create a less DEC looks very similar to Haskell views named arguments here 177 00:10:46,390 --> 00:10:51,440 and recreate the list and that just contains the value 3 178 00:10:51,440 --> 00:10:55,300 and then we calculate is like so let's run this code 179 00:10:55,300 --> 00:10:58,560 and to see if everything I told you 180 00:10:58,560 --> 00:11:01,779 is actually through an 181 00:11:01,779 --> 00:11:05,899 their Eagar 182 00:11:05,899 --> 00:11:09,649 through on it area so and 183 00:11:09,649 --> 00:11:12,980 going from top to bottom this wrong here 184 00:11:12,980 --> 00:11:16,630 is the length of and this last 185 00:11:16,630 --> 00:11:19,880 well that national days only develop you three so 186 00:11:19,880 --> 00:11:23,519 their ego if I compute one place to 187 00:11:23,519 --> 00:11:27,700 history and if I take the numbers 1 to 4 188 00:11:27,700 --> 00:11:32,700 and I'm am double down and then filter out the ones that are less than five I 189 00:11:32,700 --> 00:11:34,040 get the list 190 00:11:34,040 --> 00:11:39,370 and 24 if I print numbers 125 I get the numbers wrong 25 191 00:11:39,370 --> 00:11:43,760 and if I square 42 I get 764 192 00:11:43,760 --> 00:11:46,899 so there we go and programming got land 193 00:11:46,899 --> 00:11:51,899 if you want to learn more about got you can go to the got their website 194 00:11:51,899 --> 00:11:55,600 am and you can jump and immediately 195 00:11:55,600 --> 00:11:58,930 using all the skills you learned and haskell 196 00:11:58,930 --> 00:12:02,269 part of this lecture thank you very much and see you next time