1 00:00:01,510 --> 00:00:03,129 welcome back am 2 00:00:03,129 --> 00:00:06,150 and the first part up this a lecture 3 00:00:06,150 --> 00:00:09,519 we at seen some simple examples of 4 00:00:09,519 --> 00:00:13,210 how to define functions and has gone especially how that 5 00:00:13,210 --> 00:00:16,570 differs from most contemporary language saw 6 00:00:16,570 --> 00:00:20,830 we have seen that in Haskell conditionals are expressions not 7 00:00:20,830 --> 00:00:21,609 statements 8 00:00:21,609 --> 00:00:25,369 but thats haskell programmers rather ride 9 00:00:25,369 --> 00:00:28,849 guarded equations than conditional expressions 10 00:00:28,849 --> 00:00:32,630 the other thing we have seen is how did he find 11 00:00:32,630 --> 00:00:35,940 how to define functions using better matching 12 00:00:35,940 --> 00:00:40,950 and we have seen in particular how to define functions over lists 13 00:00:40,950 --> 00:00:45,460 using pattern matching well we're going to look at now 14 00:00:45,460 --> 00:00:49,450 is how to define functions biggest has caused a fun show language 15 00:00:49,450 --> 00:00:52,579 so we have to have a way a concise way 16 00:00:52,579 --> 00:00:56,250 to define functions and that Islam the expressions 17 00:00:56,250 --> 00:01:01,120 and you probably know /url on the expressions 18 00:01:01,120 --> 00:01:05,049 a.m. from the language that you're using a pure Java programmer 19 00:01:05,049 --> 00:01:09,450 in Java eights and along the expressions were injured used 20 00:01:09,450 --> 00:01:13,000 if you're sheesh our program error you no longer expressions 21 00:01:13,000 --> 00:01:16,030 if your hack or PHP programmer 22 00:01:16,030 --> 00:01:20,830 you have along the expressions if your javascript you have loved expression 23 00:01:20,830 --> 00:01:22,080 lund expressions are 24 00:01:22,080 --> 00:01:25,220 everywhere she plus plus you name it d 25 00:01:25,220 --> 00:01:31,229 and swift every mother language has long expressions and as we saw on the ferry 26 00:01:31,229 --> 00:01:32,170 first lecture 27 00:01:32,170 --> 00:01:35,270 edition inventions from along to church 28 00:01:35,270 --> 00:01:38,640 in the 1930s and 29 00:01:38,640 --> 00:01:41,880 most languages lund expressions look kinda similar 30 00:01:41,880 --> 00:01:45,009 and has called a look as follows: so 31 00:01:45,009 --> 00:01:48,439 day am looked like this 32 00:01:48,439 --> 00:01:52,759 on the axe arrow body of the lawn expression 33 00:01:52,759 --> 00:01:56,829 now and actual codes that you die you don't write alumni 34 00:01:56,829 --> 00:02:00,060 deride the slash the right slash X 35 00:02:00,060 --> 00:02:03,450 and is set up the arrow derides and 36 00:02:03,450 --> 00:02:08,610 minus sign and greater than but since we want these lights to look fancy 37 00:02:08,610 --> 00:02:10,229 where 38 00:02:10,229 --> 00:02:13,740 actually riding along than and a real 39 00:02:13,740 --> 00:02:17,310 bank but when you ride this thing called the right slash 40 00:02:17,310 --> 00:02:21,170 for the London and you just right the arrow 41 00:02:21,170 --> 00:02:24,890 as you would expect K so what is this thing 42 00:02:24,890 --> 00:02:28,920 this is a function addiction X and ad said 43 00:02:28,920 --> 00:02:33,580 to itself and that function has no name so it's an expression 44 00:02:33,580 --> 00:02:36,690 a function type thats Rutland expressions are 45 00:02:36,690 --> 00:02:40,220 they are expressions that the note functions 46 00:02:40,220 --> 00:02:43,370 Joanne 47 00:02:43,370 --> 00:02:46,940 the in in mathematics and and 48 00:02:46,940 --> 00:02:50,420 as church designed them are the ideal 49 00:02:50,420 --> 00:02:54,480 read along the darkness let richard eyre international on that but we tied them 50 00:02:54,480 --> 00:02:54,930 as 51 00:02:54,930 --> 00:02:58,820 backslash and instead of the arrow symbol 52 00:02:58,820 --> 00:03:02,370 we ride the aris him again 53 00:03:02,370 --> 00:03:07,740 but for the rest haskell I think it's a language that is closest to the lawn the 54 00:03:07,740 --> 00:03:08,400 cartilage 55 00:03:08,400 --> 00:03:11,890 because it's based on your functions version 56 00:03:11,890 --> 00:03:15,690 and other languages lund expressions are 57 00:03:15,690 --> 00:03:19,260 I think a better way would be to call them closures 58 00:03:19,260 --> 00:03:23,130 and we will see why because they can close over variables 59 00:03:23,130 --> 00:03:28,250 in the outer scope but the the slum the expressions can have side effects 60 00:03:28,250 --> 00:03:31,380 and but as we said it doesn't really matter 61 00:03:31,380 --> 00:03:36,420 we are really emphasizing the fact that functional programming is 62 00:03:36,420 --> 00:03:42,290 programming using expressions so why are London expressions useful 63 00:03:42,290 --> 00:03:49,290 well they're useful because they allow you to express your intent better 64 00:03:49,400 --> 00:03:54,459 when you're currying functions let's look at this first example here 65 00:03:54,459 --> 00:03:58,250 and but SL arriving at XY 66 00:03:58,250 --> 00:04:01,959 because X plus why should this occur redefinition and 67 00:04:01,959 --> 00:04:05,600 this is a function that they should parameter axe 68 00:04:05,600 --> 00:04:08,780 readers a function that takes a barometer why 69 00:04:08,780 --> 00:04:12,510 and then returns X plus Y really 70 00:04:12,510 --> 00:04:16,160 the intent is better expressed by this 71 00:04:16,160 --> 00:04:19,320 at is a function 72 00:04:19,320 --> 00:04:24,630 it takes bram interact readers a function that takes a brother why 73 00:04:24,630 --> 00:04:27,750 and then add them together okay 74 00:04:27,750 --> 00:04:30,830 so this is actually syntactic sugar 75 00:04:30,830 --> 00:04:35,669 fordice and when I write haskell I often write them in this for 76 00:04:35,669 --> 00:04:38,880 even though it's a little bit longer 77 00:04:38,880 --> 00:04:42,479 at means that I have to type more characters 78 00:04:42,479 --> 00:04:46,680 but I think this for is more readable than that for 79 00:04:46,680 --> 00:04:50,039 am and in the course of 80 00:04:50,039 --> 00:04:53,860 of this these lectures we'll switch between 81 00:04:53,860 --> 00:04:58,350 both of these and but again if you look at my haskell God 82 00:04:58,350 --> 00:05:03,220 I prefer to write things using a splash at long last to convey 83 00:05:03,220 --> 00:05:09,590 the actual meaning of the current function and expressions 84 00:05:09,590 --> 00:05:13,419 are also useful when you want to and 85 00:05:13,419 --> 00:05:17,110 make explicit that you're returning a function that's 86 00:05:17,110 --> 00:05:20,930 a camp some sense the same scurrying but here 87 00:05:20,930 --> 00:05:24,539 Ikea if we define a constant function 88 00:05:24,539 --> 00:05:29,710 so this is a function and given a will return a function that will never be 89 00:05:29,710 --> 00:05:34,490 you gave it will return that day so %eh defined goals backs 90 00:05:34,490 --> 00:05:39,380 readers a function that will whatever you give its just ignore it 91 00:05:39,380 --> 00:05:43,990 and return X okay so that constant function there 92 00:05:43,990 --> 00:05:47,660 is very concisely defined using along the expression 93 00:05:47,660 --> 00:05:50,740 where's here I think it's a little bit less clear 94 00:05:50,740 --> 00:05:53,940 gone still backs underscore equals x 95 00:05:53,940 --> 00:05:58,020 so this round doesn't make explicit defect 96 00:05:58,020 --> 00:06:02,160 that you're returning a function so he is also emphasized by putting the 97 00:06:02,160 --> 00:06:03,120 parentheses 98 00:06:03,120 --> 00:06:06,300 around died and here you don't do that 99 00:06:06,300 --> 00:06:09,500 so I'll this is how I were dry dysfunction I would 100 00:06:09,500 --> 00:06:12,840 with the brand she's on the type and I puts the 101 00:06:12,840 --> 00:06:18,099 writer function as a lawn the expression am 102 00:06:18,099 --> 00:06:21,650 here's another you shove using on the expressions 103 00:06:21,650 --> 00:06:25,530 and that's to avoid inventing names 104 00:06:25,530 --> 00:06:27,889 inventing names is 105 00:06:27,889 --> 00:06:32,259 one of the hardest things that we have to do it programming in 106 00:06:32,259 --> 00:06:35,389 coming up meaningful names because often 107 00:06:35,389 --> 00:06:40,580 you know their language forces you to name something where you just don't want 108 00:06:40,580 --> 00:06:42,990 to name it just want to bash the thing directly 109 00:06:42,990 --> 00:06:46,330 so for example if I want to map a function 110 00:06:46,330 --> 00:06:50,539 over a list there's no reason I should get that function here 111 00:06:50,539 --> 00:06:54,520 name and what this have say anyway 112 00:06:54,520 --> 00:06:57,810 so instead to what I can do again just 113 00:06:57,810 --> 00:07:00,909 Obama's love the expression do the map 114 00:07:00,909 --> 00:07:04,340 and that will it be used to map this function 115 00:07:04,340 --> 00:07:07,689 over the list right show this 116 00:07:07,689 --> 00:07:11,370 not idiomatic national this 117 00:07:11,370 --> 00:07:15,279 idiomatic really the only way 118 00:07:15,279 --> 00:07:19,979 place are you want to name something is when you use it more than once 119 00:07:19,979 --> 00:07:24,650 or if it becomes clear but thats 120 00:07:24,650 --> 00:07:28,839 yeah up to interpretation here's another 121 00:07:28,839 --> 00:07:32,889 all the tea of Haskell and that's called sections 122 00:07:32,889 --> 00:07:36,589 so with sections you can take 123 00:07:36,589 --> 00:07:39,919 a binary operator in and fix operate like plus 124 00:07:39,919 --> 00:07:43,089 and use it as a function so by putting 125 00:07:43,089 --> 00:07:47,589 a ranch around it you can use plus as a function so one plus two 126 00:07:47,589 --> 00:07:51,629 it was three and plus in brands 127 00:07:51,629 --> 00:07:56,599 12 is also tree now this is a nice trick 128 00:07:56,599 --> 00:08:01,399 because we can also foods Wong 129 00:08:01,399 --> 00:08:04,839 a brand one-plus and we can do 130 00:08:04,839 --> 00:08:08,460 plus upper and blessed to show weekend section began 131 00:08:08,460 --> 00:08:12,029 partially apply plus by putting 132 00:08:12,029 --> 00:08:15,180 Iran's around the plush so here we see 133 00:08:15,180 --> 00:08:18,699 we can do one-plus which is a function 134 00:08:18,699 --> 00:08:22,020 that expect second argument I will 135 00:08:22,020 --> 00:08:25,339 at 12 it and here we have to function 136 00:08:25,339 --> 00:08:30,099 section plus do which is a function that expects 137 00:08:30,099 --> 00:08:33,479 value here and then added 138 00:08:33,479 --> 00:08:37,829 to ensure this is one place to equals three show in general 139 00:08:37,829 --> 00:08:39,560 F 140 00:08:39,560 --> 00:08:43,279 is a by a plus funny place is a binary operator 141 00:08:43,279 --> 00:08:47,100 then you can ride plus and brands 142 00:08:47,100 --> 00:08:50,129 to use it as a normal function 143 00:08:50,129 --> 00:08:53,189 you can left or right section head by 144 00:08:53,189 --> 00:08:56,389 partially applying it to an argument K 145 00:08:56,389 --> 00:09:00,509 sewer this is something that you will see while all the National 146 00:09:00,509 --> 00:09:04,759 specially here if you want to have a function that anchormen something 147 00:09:04,759 --> 00:09:11,029 just right one-plus as a section to wire sections useful 148 00:09:11,029 --> 00:09:16,249 well here are a couple of examples you on the function that God fiction 149 00:09:16,249 --> 00:09:18,059 argument and hot shit to all 150 00:09:18,059 --> 00:09:21,999 you just ride opened brand divided by two 151 00:09:21,999 --> 00:09:25,199 and you don't have to ride lumber axe 152 00:09:25,199 --> 00:09:29,709 axe divided by two so sectioning allows you to write something 153 00:09:29,709 --> 00:09:32,990 read-out inventing an and thats 154 00:09:32,990 --> 00:09:36,360 said the whole goal of this exercise 155 00:09:36,360 --> 00:09:39,749 giving names is hard so we want to avoid that 156 00:09:39,749 --> 00:09:44,649 and Section allows you to do that alright 157 00:09:44,649 --> 00:09:51,120 so I am here's a preview of the exercises the exercises will be 158 00:09:51,120 --> 00:09:52,649 available on the website 159 00:09:52,649 --> 00:09:55,939 and these exercises real ask you 160 00:09:55,939 --> 00:09:59,759 to use am some of these constructs that 161 00:09:59,759 --> 00:10:03,410 a.m. we have seen in this lecture to define 162 00:10:03,410 --> 00:10:07,600 functions and as I promise you this will be the last 163 00:10:07,600 --> 00:10:10,860 and exercise that's very haskell specific 164 00:10:10,860 --> 00:10:14,819 but since you now we we have been talking about how haskell 165 00:10:14,819 --> 00:10:18,689 differs from other languages I think it's quite useful 166 00:10:18,689 --> 00:10:23,490 an to follow the rest of the lectures to do these exercises 167 00:10:23,490 --> 00:10:26,809 to get a little bit familiar an 168 00:10:26,809 --> 00:10:31,110 with the Haskell syntex am and so happy hacking 169 00:10:31,110 --> 00:10:33,689 and see you next week bye bye