1 00:00:01,189 --> 00:00:03,649 ever ready welcome back to 2 00:00:03,649 --> 00:00:07,149 ft 10 on ax functional programming 3 00:00:07,149 --> 00:00:10,340 an and over granted 4 00:00:10,340 --> 00:00:14,009 to do today is taking the first 5 00:00:14,009 --> 00:00:18,160 baby steps in learning haskell now 6 00:00:18,160 --> 00:00:21,390 one of my favorite movies of all time 7 00:00:21,390 --> 00:00:25,890 is the Karate Kid and if you haven't seen this movie 8 00:00:25,890 --> 00:00:29,170 stop this yeah watching 9 00:00:29,170 --> 00:00:32,200 this lecture and go watch this movie first 10 00:00:32,200 --> 00:00:37,030 every time I need to learn something new I rewatched the Karate Kid 11 00:00:37,030 --> 00:00:40,730 and I can tell you I've seen it you know maybe 12 00:00:40,730 --> 00:00:44,210 20-30 times and every time i watch it 13 00:00:44,210 --> 00:00:47,820 I learn something new if you're 14 00:00:47,820 --> 00:00:50,890 a watch the movie you will see that 15 00:00:50,890 --> 00:00:55,500 the and the boy here Danielle and wants to learn karate 16 00:00:55,500 --> 00:00:59,390 and mister me I got it the gentlemen here 17 00:00:59,390 --> 00:01:03,320 I'm on the other side and make him do a lot of 18 00:01:03,320 --> 00:01:08,320 what and Danielle things are pointless exercise any gets kinda frustrated 19 00:01:08,320 --> 00:01:11,400 but in order to learn something you have to go 20 00:01:11,400 --> 00:01:15,650 through a lot of exercises because there's love muscle memory or and 21 00:01:15,650 --> 00:01:20,049 case of programming you have to have the muscle memory in your fingers they're 22 00:01:20,049 --> 00:01:22,950 certain things should not have to think about 23 00:01:22,950 --> 00:01:27,520 that you have to drill that you have to do over and over and over again 24 00:01:27,520 --> 00:01:32,430 and that's what we are going to do in this lecture today 25 00:01:32,430 --> 00:01:36,070 don't get frustrated don't be like Daniel 26 00:01:36,070 --> 00:01:40,280 be a grownup and just do your exercises 27 00:01:40,280 --> 00:01:44,420 on and on wax on wax off wax on wax off 28 00:01:44,420 --> 00:01:47,640 until it becomes completely automatic 29 00:01:47,640 --> 00:01:51,450 and then you can use your brain to solve the real problem 30 00:01:51,450 --> 00:01:55,649 you don't have to think about the basics any okay and every time you learn 31 00:01:55,649 --> 00:01:56,549 something new 32 00:01:56,549 --> 00:02:01,049 re watch this great movie alright 33 00:02:01,049 --> 00:02:05,560 and show some ask has complementation that we're using 34 00:02:05,560 --> 00:02:10,319 in this course a is the Glasgow Haskell Compiler 35 00:02:10,319 --> 00:02:13,530 sometimes also called the glorious Haskell Compiler 36 00:02:13,530 --> 00:02:18,190 > is the leading implementation of Haskell 37 00:02:18,190 --> 00:02:23,540 and it has both a compiler an interactive 38 00:02:23,540 --> 00:02:26,900 Rep a loop that you can use to typing your 39 00:02:26,900 --> 00:02:30,310 got and the interactive nature 40 00:02:30,310 --> 00:02:34,300 of the GATT interpreter makes it really easy 41 00:02:34,300 --> 00:02:37,459 to experiment and with has car 42 00:02:37,459 --> 00:02:40,769 and that's why it's also very useful for discourse 43 00:02:40,769 --> 00:02:44,330 am many of the exercises 44 00:02:44,330 --> 00:02:47,790 that you will get during the course you can do 45 00:02:47,790 --> 00:02:51,519 by using ght I don't care I don't 46 00:02:51,519 --> 00:02:54,800 see that as cheating an because 47 00:02:54,800 --> 00:02:58,730 as programmers reshoot really use to compiler 48 00:02:58,730 --> 00:03:04,420 to help us okay so the computer is there to help us programmed the computer 49 00:03:04,420 --> 00:03:08,660 if we don't use that that would be stupid GT 50 00:03:08,660 --> 00:03:14,079 is freely available from that website here there will be also be on the 51 00:03:14,079 --> 00:03:17,880 an course website sorry you can also 52 00:03:17,880 --> 00:03:21,220 Google or Bing for the Haskell platform 53 00:03:21,220 --> 00:03:24,780 and it's available for all major platforms 54 00:03:24,780 --> 00:03:29,910 Mac when does and Linux to start GHz 55 00:03:29,910 --> 00:03:33,840 you just type from the command line Ghei 56 00:03:33,840 --> 00:03:37,549 and then you get some 57 00:03:37,549 --> 00:03:41,230 text blur and there you see a bronze 58 00:03:41,230 --> 00:03:44,970 preludes and that's where you can start typing an 59 00:03:44,970 --> 00:03:49,180 expressions for example we can type in expression to 60 00:03:49,180 --> 00:03:53,150 plus three times for and result if that's the 61 00:03:53,150 --> 00:03:57,690 Ghei will then printed the result for that in this case were really using 62 00:03:57,690 --> 00:04:01,380 GA CIS gotta day calculator 63 00:04:01,380 --> 00:04:05,250 it's a little bit I can now calculate their own a command line interface 64 00:04:05,250 --> 00:04:10,340 and set up a girl getting a red buttons well we also see here's that since 65 00:04:10,340 --> 00:04:13,680 to last three times for apparently age 14 66 00:04:13,680 --> 00:04:17,459 that an not obligation bind stronger 67 00:04:17,459 --> 00:04:20,640 then addition as so there's no surprises there 68 00:04:20,640 --> 00:04:24,450 most languages have that and everyone to 69 00:04:24,450 --> 00:04:28,450 at first duty addition and multiplication we have two boots 70 00:04:28,450 --> 00:04:31,510 a.m. brand to see a show we ride to blustery 71 00:04:31,510 --> 00:04:35,910 I'm for and then there is all this trendy and we can do 72 00:04:35,910 --> 00:04:40,880 more complicated expressions like the square root of dead three to the power 73 00:04:40,880 --> 00:04:41,630 of two 74 00:04:41,630 --> 00:04:46,690 for to the bar of two and we get five am 75 00:04:46,690 --> 00:04:50,100 as you saw there was is it gonna be a the prelude 76 00:04:50,100 --> 00:04:53,490 and and from 77 00:04:53,490 --> 00:04:58,260 here so and at school prelude it's not the SDK for Haskell these are all the 78 00:04:58,260 --> 00:04:59,390 standard functions 79 00:04:59,390 --> 00:05:03,330 that come for free and what has gone on the website 80 00:05:03,330 --> 00:05:07,930 you will we will tell you how to get other libraries for Haskell 81 00:05:07,930 --> 00:05:13,620 but an rewards discuss that in too much detail right here you 82 00:05:13,620 --> 00:05:16,740 you can read up on that we own will 83 00:05:16,740 --> 00:05:20,840 will try to stake with just this standard library functions standard 84 00:05:20,840 --> 00:05:21,390 values 85 00:05:21,390 --> 00:05:27,680 and most of them are most influential order to function in the prelude 86 00:05:27,680 --> 00:05:30,710 are functions over lists am 87 00:05:30,710 --> 00:05:34,750 and we're going to get walked through a couple of them and then later lectures 88 00:05:34,750 --> 00:05:38,810 we well implemented these functions in several different 89 00:05:38,810 --> 00:05:42,500 manners the first function years had which takes a list 90 00:05:42,500 --> 00:05:46,350 and the way the right mission has college with square brackets and the 91 00:05:46,350 --> 00:05:48,280 elements separated by commas 92 00:05:48,280 --> 00:05:52,030 showed ahead of wanted 3-for-5 s 93 00:05:52,030 --> 00:05:56,110 and surprisingly Wong the tail 94 00:05:56,110 --> 00:06:00,130 that's the opposite of Hatcher it is everything on the list 95 00:06:00,130 --> 00:06:03,340 accept that so detail wanted 3-for-5 96 00:06:03,340 --> 00:06:07,710 is the list to treat for we can also select 97 00:06:07,710 --> 00:06:11,360 and the and element of the list which this year 98 00:06:11,360 --> 00:06:16,250 double action mission mark operators are if we want to select the second element 99 00:06:16,250 --> 00:06:17,160 of the list 100 00:06:17,160 --> 00:06:20,320 I wanted 3-for-5 we get 101 00:06:20,320 --> 00:06:24,030 3 so what do we learn from this well 102 00:06:24,030 --> 00:06:27,029 that the first element of the list 103 00:06:27,029 --> 00:06:29,610 as index 0 K so 104 00:06:29,610 --> 00:06:32,749 like it should be haskell starch 105 00:06:32,749 --> 00:06:36,849 indexing lists by zero now there's 106 00:06:36,849 --> 00:06:40,409 to exclamation marks here and there's 107 00:06:40,409 --> 00:06:44,159 a good reason for that biggest in Haskell 108 00:06:44,159 --> 00:06:47,189 lists are not to raise show indexing 109 00:06:47,189 --> 00:06:50,909 into the analyst here at the second element 110 00:06:50,909 --> 00:06:54,729 will traverse the Leisure World reversal estro a long 111 00:06:54,729 --> 00:06:59,509 throw a to and then returned three so this is not a constant I'm 112 00:06:59,509 --> 00:07:04,209 operation to indexing into a list unlike induction into an array 113 00:07:04,209 --> 00:07:07,919 and Haskell is then they ran the length of the list 114 00:07:07,919 --> 00:07:13,099 are and then there and and this number but in general linear in the size of the 115 00:07:13,099 --> 00:07:13,599 list 116 00:07:13,599 --> 00:07:17,509 and but fortunately 117 00:07:17,509 --> 00:07:21,180 an reveal hardly see uses 118 00:07:21,180 --> 00:07:24,629 of a this operator in practice and 119 00:07:24,629 --> 00:07:28,550 if you find yourself wanting to use less than texting 120 00:07:28,550 --> 00:07:34,180 then you know that you have not done than of wax on wax off wax on wax off 121 00:07:34,180 --> 00:07:37,569 you're still too much in the mindset of 122 00:07:37,569 --> 00:07:40,689 impaired if programming using a race and 123 00:07:40,689 --> 00:07:45,189 and Haskell we typically use higher order functions like Matt filter fault 124 00:07:45,189 --> 00:07:48,300 and not indexing to a.m. 125 00:07:48,300 --> 00:07:53,119 perform complex operations over list here's another 126 00:07:53,119 --> 00:07:59,719 an example take three it takes the first three element of a list so if we have 127 00:07:59,719 --> 00:08:01,180 the list went 3-for-5 128 00:08:01,180 --> 00:08:04,680 guess what readers the first three elements 129 00:08:04,680 --> 00:08:08,139 123 drop 130 00:08:08,139 --> 00:08:11,619 is the opposite side takes a number 131 00:08:11,619 --> 00:08:16,719 and the list and it drops the first three elements of the list so in some 132 00:08:16,719 --> 00:08:17,769 sense drop 133 00:08:17,769 --> 00:08:20,819 and take are 134 00:08:20,819 --> 00:08:24,300 the generalizations of head and tail at entail 135 00:08:24,300 --> 00:08:29,159 just take the first and the rest of the last and then which day can drop 136 00:08:29,159 --> 00:08:33,130 you can you now you can generalized that too arbitrary numbers 137 00:08:33,130 --> 00:08:36,370 and calculating the length of the last 138 00:08:36,370 --> 00:08:40,459 alright we do that by saying Lancs 139 00:08:40,459 --> 00:08:44,930 the going to function length length wanted 3-for-5 is five 140 00:08:44,930 --> 00:08:49,180 and again and lesson at school are not like a race 141 00:08:49,180 --> 00:08:53,910 in other languages so taking the length is not a constant I'm operation 142 00:08:53,910 --> 00:08:58,070 it is takes let its time linear in the length of the list 143 00:08:58,070 --> 00:09:04,890 and some sums up all the elements show every goal some unwanted 345 144 00:09:04,890 --> 00:09:09,040 the result is fifty product 145 00:09:09,040 --> 00:09:12,570 is the same as some acceptance data adding up the numbers 146 00:09:12,570 --> 00:09:17,080 multiplies all of them so want to multiplying long 147 00:09:17,080 --> 00:09:21,520 to three four and five like taking effect or l5 148 00:09:21,520 --> 00:09:27,649 is 120 offending two lists is another common operation you take to less the 149 00:09:27,649 --> 00:09:30,290 smashing together into a new list so I 150 00:09:30,290 --> 00:09:33,570 a band 123 with four and five 151 00:09:33,570 --> 00:09:38,660 we get the less wanted three for $5 and the loss function here 152 00:09:38,660 --> 00:09:43,220 on this and slides is reversing a list 153 00:09:43,220 --> 00:09:46,260 reverse 1,2,3,4,5 it's you 154 00:09:46,260 --> 00:09:50,380 5 for 321 in the rest of this 155 00:09:50,380 --> 00:09:54,990 these lectures we will define these all the functions ourselves 156 00:09:54,990 --> 00:09:58,300 in several ways an but right now 157 00:09:58,300 --> 00:10:01,600 it's good to kind of play with them to see what they do 158 00:10:01,600 --> 00:10:05,040 an and then we will be ready to implement them 159 00:10:05,040 --> 00:10:08,640 an in our then we will know other supposed to do 160 00:10:08,640 --> 00:10:12,380 here's the 161 00:10:12,380 --> 00:10:16,260 and another very important slight 162 00:10:16,260 --> 00:10:20,839 and that is about function application because has girl 163 00:10:20,839 --> 00:10:24,450 is very different than mathematics on the one side 164 00:10:24,450 --> 00:10:27,700 and very different from other programming languages as well 165 00:10:27,700 --> 00:10:32,910 in mathematics when you apply function mathematics that you do in school or 166 00:10:32,910 --> 00:10:34,860 college when you take analysis 167 00:10:34,860 --> 00:10:38,100 when you apply a function to an argument 168 00:10:38,100 --> 00:10:41,649 you always use parentheses so in this case 169 00:10:41,649 --> 00:10:45,100 when we apply a function here this is 170 00:10:45,100 --> 00:10:49,410 applying a function have to arguments a NB 171 00:10:49,410 --> 00:10:52,180 and then adding C 172 00:10:52,180 --> 00:10:55,910 space D now in mathematics 173 00:10:55,910 --> 00:10:59,460 typically art and that's gone from physics 174 00:10:59,460 --> 00:11:03,720 is that space mange multiplication 175 00:11:03,720 --> 00:11:07,300 right so if you look at the mathematical textbook 176 00:11:07,300 --> 00:11:11,430 and you see ya see space D 177 00:11:11,430 --> 00:11:14,800 usually need see multiplied by D 178 00:11:14,800 --> 00:11:18,480 and therefore when you apply a function you're using 179 00:11:18,480 --> 00:11:24,330 Prentice's like this to to disambiguate since in Haskell 180 00:11:24,330 --> 00:11:27,530 function application is used much more often 181 00:11:27,530 --> 00:11:30,990 then multiplication virus in physics 182 00:11:30,990 --> 00:11:34,630 and multiplication is used more often so that's where 183 00:11:34,630 --> 00:11:40,080 not obligation is given the Kaneohe the most whites involved at least gonna 184 00:11:40,080 --> 00:11:43,680 visible symbolic motion white spaces Mr and Haskell 185 00:11:43,680 --> 00:11:46,680 function application is denoted by white space 186 00:11:46,680 --> 00:11:50,050 so that it can if it's not so much in your face 187 00:11:50,050 --> 00:11:54,050 when haskell and celebrating at of a call might be 188 00:11:54,050 --> 00:11:58,960 plus she space D function application becomes 189 00:11:58,960 --> 00:12:02,660 denoted by a spatial its after apply to a 190 00:12:02,660 --> 00:12:06,180 apply to be plus see times the 191 00:12:06,180 --> 00:12:09,540 alright and most programming languages 192 00:12:09,540 --> 00:12:13,790 you would ride at for an 193 00:12:13,790 --> 00:12:18,540 a go might be goes for so they are in some sense the world 194 00:12:18,540 --> 00:12:23,680 worst of two worlds their are your your have to write everything explicit 195 00:12:23,680 --> 00:12:27,970 the National we make that decision that function application 196 00:12:27,970 --> 00:12:31,900 is done by using a space and multiplication 197 00:12:31,900 --> 00:12:35,500 is done by using multiplication 198 00:12:35,500 --> 00:12:39,080 and also 199 00:12:39,080 --> 00:12:42,480 function application by stronger 200 00:12:42,480 --> 00:12:45,790 then any other operator so if you see af 201 00:12:45,790 --> 00:12:49,820 of a place be that means after applied to a 202 00:12:49,820 --> 00:12:52,970 plus be doesn't mean af 203 00:12:52,970 --> 00:12:57,250 applied to a-plus be game disc and get 204 00:12:57,250 --> 00:13:01,080 yeah people have to get used to this this doesn't mean 205 00:13:01,080 --> 00:13:04,210 after dimes a-plus be it means 206 00:13:04,210 --> 00:13:05,350 F apply 207 00:13:05,350 --> 00:13:08,400 to a blessed be so that's 208 00:13:08,400 --> 00:13:12,370 got similar to multiplication binding stronger than addition 209 00:13:12,370 --> 00:13:16,800 okay here's a table that give you sum up the 210 00:13:16,800 --> 00:13:20,210 am problem more expressions 211 00:13:20,210 --> 00:13:23,410 and on that one side's haskell 212 00:13:23,410 --> 00:13:27,750 and on the other side mathematics so this is what you should forget 213 00:13:27,750 --> 00:13:31,240 I'm going to stand in front of that this is what you should forget 214 00:13:31,240 --> 00:13:34,320 and this is what you should learn alright 215 00:13:34,320 --> 00:13:38,590 so forget what's behind me and learn 216 00:13:38,590 --> 00:13:42,670 what's on this site okay so 217 00:13:42,670 --> 00:13:45,890 instead of after opened brand actually write 218 00:13:45,890 --> 00:13:50,420 at SpaceX instead of passing two parameters to at 219 00:13:50,420 --> 00:13:55,230 ripon we apply have to action and then apply that revealed to why 220 00:13:55,230 --> 00:14:00,540 instead of at opened brand GOP brown actually can ride 221 00:14:00,540 --> 00:14:04,110 at of geoVax and later we'll see 222 00:14:04,110 --> 00:14:07,120 that we can write that using function composition 223 00:14:07,120 --> 00:14:11,680 have as as at combos G okay 224 00:14:11,680 --> 00:14:14,940 and their and the rest up the examples 225 00:14:14,940 --> 00:14:18,720 should be obvious okay but if you just look at 226 00:14:18,720 --> 00:14:22,680 shapes have these two don't try to interpret them you will see that the 227 00:14:22,680 --> 00:14:24,300 Haskell code here 228 00:14:24,300 --> 00:14:27,650 looks much lighter weight than the code 229 00:14:27,650 --> 00:14:33,070 on this side and Haskell programmers are lazy we don't want to dive too much 230 00:14:33,070 --> 00:14:36,470 so this looks much more beautiful and this 231 00:14:36,470 --> 00:14:39,860 and that's why Azkals index is designed the way it is 232 00:14:39,860 --> 00:14:43,350 thank you this was the end of Part long 233 00:14:43,350 --> 00:14:46,680 see you in a bit for the remainder of 234 00:14:46,680 --> 00:14:47,320 kept their own